2016-09-28 6 views
0

Я использую плагин eclipse, у меня есть 3 hasmap-имя, например hmTextONLY, hmCOMBO1ONLY, hmCOMBO2ONLY (его содержит ключ, значение), тогда я хочу сделать простой связанныйhashmap, используя тот же ключ из выше hashmap, а другая вещь - индекс понравившегосяhashmap и ключ хэш-карты такие же.Как слить 3 hashmap в single linkedhasmap?

3 код HashMap ниже:

private void fillRows(String shortdesc, String categ, String descp) { 
    TableItem ramtableitem = new TableItem(table, SWT.NONE); 
    // for Threat_Name 
    TableEditor editorTN = new TableEditor(table); 
    text_1 = new Text(table, SWT.NONE); 
    editorTN.grabHorizontal = true; 
    editorTN.setEditor(text_1, ramtableitem, 0); 
    text_1.setText(shortdesc); 
    Theart_Name = text_1.getText(); 
    hmTextONLY.put(hmT++, Theart_Name); 


    // For Category_Name 
    TableEditor editorCN = new TableEditor(table); 
    text_2 = new Text(table, SWT.NONE); 
    editorCN.grabHorizontal = true; 
    editorCN.setEditor(text_2, ramtableitem, 1); 
    text_2.setText(categ); 
    Category_Name = text_2.getText(); 
    hmTextONLY.put(hmT++, Category_Name); 

    String items[] = { "Mitigated", "Not Applicable", "Not Started", 
     "Needs Investigation" }; 
    Arrays.sort(items); 

    final CCombo Status_Combo = new CCombo(table, SWT.NONE); 
    Status_Combo.setItems(items); 
    TableEditor editor = new TableEditor(table); 
    editor.grabHorizontal = true; 
    editor.setEditor(Status_Combo, ramtableitem, 2); 

    Status_Combo.addSelectionListener(new SelectionListener() { 
     public void widgetSelected(SelectionEvent e) { 
     Status_Name = Status_Combo.getText(); 
     hmCOMBO1ONLY.put(hmC1, Status_Name); 
    } 

    public void widgetDefaultSelected(SelectionEvent e) {    
     Status_Name = Status_Combo.getText();    
     hmCOMBO1ONLY.put(hmC1, Status_Name); 
     } 
    }); 


    // For Priority_Name 
    String itemss[] = { "High", "Medium", "Low" }; 
     Arrays.sort(itemss); 
    final CCombo Priority_Combo = new CCombo(table, SWT.NONE); 
    Priority_Combo.setItems(itemss); 
     TableEditor editorP = new TableEditor(table); 
    editorP.grabHorizontal = true; 
    editorP.setEditor(Priority_Combo, ramtableitem, 3); 

    Priority_Combo.addSelectionListener(new SelectionListener() { 
    public void widgetSelected(SelectionEvent e) { 
     System.out.println(Priority_Combo.getText()); 
     Priority_Name = Priority_Combo.getText(); 
     hmCOMBO2ONLY.put(hmC2, Priority_Name); 
    } 

    public void widgetDefaultSelected(SelectionEvent e) { 
     System.out.println(Priority_Combo.getText()); 
     Priority_Name = Priority_Combo.getText(); 
     hmCOMBO2ONLY.put(hmC2, Priority_Name); 
     } 
    }); 
    // For Descrption_Name 
    TableEditor editorDN = new TableEditor(table); 
    text_3 = new Text(table, SWT.NONE); 
    editorDN.grabHorizontal = true; 
    editorDN.setEditor(text_3, ramtableitem, 4); 
    text_3.setText(descp); 
    Descrption_Name = text_3.getText(); 
    hmTextONLY.put(hmT++, Descrption_Name); 

    // For justification 
    TableEditor editorJ = new TableEditor(table); 
    text_4 = new Text(table, SWT.MULTI | SWT.BORDER | SWT.WRAP 
     | SWT.V_SCROLL); 
    editorJ.grabHorizontal = true; 
    editorJ.setEditor(text_4, ramtableitem, 5); 
    Justification_Name = text_4.getText().toString().trim(); 
    hmTextONLY.put(hmT++, Justification_Name); 

} 

другой метод SaveState, где я хочу, чтобы объединить все HashMap в LinkedHashMap. , приведенный ниже.

public void saveState(IMemento memento) { 

    super.saveState(memento); 
    System.out.println("Save State Called"); 
    System.out.println("row" + table.getItemCount()); 
    System.out.println("hmTextONLY.size()=====" + hmTextONLY.size()); 
    Map<Integer, String> linkedHashMap = new LinkedHashMap<>(); 
} 
+0

вы пробовали [ 'putAll'] (https://docs.oracle .com/JavaSE/8/документы/API/Java/Util/Map.html # putAll-java.util.Map-) – fabian

ответ

0

LinkedHashMap имеет вставки порядок ключей, так что вы можете сортировать карты, а затем добавить их к связанной карте:

TreeMap<Integer, String> sortedMap = new TreeMap<>(); 
sortedMap.putAll(hmTextONLY); 
sortedMap.putAll(hmCOMBO1ONLY); 
sortedMap.putAll(hmCOMBO2ONLY); 
Map<Integer, String> linkedHashMap = new LinkedHashMap<Integer,String>(sortedMap);