2012-05-23 49 views
0

我的取值從地圖來像下面如何根據某些特定值對地圖中的值進行分組?

Key = 1_1, Value = 02/04/2012 
Key = 1_2, Value = 03/04/2012 
Key = 1_3, Value = 04/04/2012 
Key = 1_4, Value = 05/04/2012 
Key = 1_5, Value = 06/04/2012 
Key = 1_6, Value = 09/04/2012 
Key = 1_7, Value = 10/04/2012 
Key = 1_8, Value = 11/04/2012 
Key = 1_9, Value = 12/04/2012 
Key = 1_10, Value = 13/04/2012 
Key = 1_11, Value = 18/04/2012 
Key = 1_12, Value = 19/04/2012 
Key = 1_13, Value = 20/04/2012 
Key = 1_14, Value = 23/04/2012 
Key = 1_15, Value = 24/04/2012 
Key = 1_16, Value = 25/04/2012 
Key = 1_17, Value = 26/04/2012 
Key = 1_18, Value = 27/04/2012 
Key = 1_19, Value = 30/04/2012 
Key = 10_20, Value = 02/04/2012 
Key = 10_21, Value = 03/04/2012 
Key = 10_22, Value = 04/04/2012 
Key = 10_23, Value = 05/04/2012 
Key = 10_24, Value = 06/04/2012 
Key = 10_25, Value = 09/04/2012 
Key = 10_26, Value = 10/04/2012 
Key = 10_27, Value = 11/04/2012 
Key = 10_28, Value = 12/04/2012 
Key = 10_29, Value = 13/04/2012 
Key = 10_30, Value = 16/04/2012 
Key = 10_31, Value = 17/04/2012 
Key = 10_32, Value = 18/04/2012 
Key = 10_33, Value = 19/04/2012 
Key = 10_34, Value = 23/04/2012 
Key = 10_35, Value = 24/04/2012 
Key = 10_36, Value = 26/04/2012 
Key = 10_37, Value = 27/04/2012 

我真的很努力分開這些值,並把它們放在單獨的地圖。

我想分組如下。

1_1到1_19這個我想根據「_」進行分割並單獨獲得第一個值並將它們分組到一個單獨的地圖中。

像1是關鍵,值將是日期。

編輯:

employeeMap = showExelData(sheetData); 
     String previousEemployeeID = "",employeeID[]; 
     Iterator<Map.Entry> entries = employeeMap.entrySet().iterator(); 
     while (entries.hasNext()) { 
      Map.Entry entry = entries.next(); 
      employeeID = entry.getKey().toString().split("_"); 

       // this is the place where i want to check the values if 1 than group the values it can be even Key = 1_0, Value = 25/04/2012 to If Key = 1_18, Value = 30/04/2012 
    but when the other one comes ex : Key = 10_0, Value = 25/04/2012 to If Key = 10_17, Value = 30/04/2012it has to go to new Map 

我這是在哪裏欠缺的地方。 }

+1

'HashMap's要求'key'是唯一的。有19個等於'1'的鍵不能用於HashMap。 – npinti

+0

你是相當不確定的,你的意思是「將它們分組在一張單獨的地圖中」。你想要一個'Map >'這樣'outer.get(「1」)。get(「3」)。equals(「04/04/2012」)'?或者你更喜歡一個'Map >',每個鍵只有幾個值。 –

+0

@nd感謝評論,我有幾個值如上所述,我想將這些值在單獨的地圖,因爲它是,但一個地圖將有整個值設置爲例如:1_1到1_19和第二張地圖將有10_1到10_x同樣我想分組他們。 – SAR

回答

2

如果您想將1_1,1_2拆分爲1_19 ..請使用String類的split()函數。

Ex。

String x = 1_19; 

String[] y = x.split("_"); 

Y [0]將是等於1且y [1]將19

至於使用所述第一值用於在地圖的密鑰,這將是不可能的,因爲它需要一地圖中每個條目的唯一鍵,就像npinti在您的帖子中發表的評論一樣。

public class Mapping { 

    Map<String, String> coMap; 
    List<String> coList; 

    public Mapping() 
    { 
     init(); 
    } 

    public static void main(String[] args) 
    { 
     Mapping oMapping = new Mapping(); 

     Map<String, Map<String, String>> oMap = oMapping.classifyMapEntries(); 

     for (String sParentKey : oMapping.coList) 
     { 
      Map<String, String> oChildMap = oMap.get(sParentKey); 
      Iterator<String> oIterator = oChildMap.keySet().iterator(); 

      System.out.println("Map"); 
      while(oIterator.hasNext()) 
      { 
       String sChildKey = oIterator.next(); 
       System.out.print("Key: " + sChildKey + ", Value: " 
            + oChildMap.get(sChildKey) + "\n"); 
      } 
     } 
    } 

    private void init() 
    { 
     coMap = new HashMap<String, String>(); 
     coList = new ArrayList<String>(); 

     coMap.put("1_1", "a"); 
     coMap.put("1_19", "a"); 
     coMap.put("10_1", "b"); 
     coMap.put("10_19", "b"); 
    } 

    private Map<String, Map<String, String>> classifyMapEntries() 
    { 
     Map<String, Map<String, String>> oClassified = 
      new HashMap<String, Map<String,String>>(); 

     Iterator<String> oIterator = coMap.keySet().iterator(); 
     while(oIterator.hasNext()) 
     { 
      String sKey = oIterator.next(); 

      String sFirst = sKey.substring(0,sKey.indexOf("_")); 
      if (!coList.contains(sFirst)) 
      { 
       coList.add(sFirst); 
      } 
     } 

     for (String sKey : coList) 
     { 
      Map<String, String> oChildMap = new HashMap<String, String>(); 

      Iterator<String> oIterator2 = coMap.keySet().iterator(); 
      while(oIterator2.hasNext()) 
      { 
       String sChildKey = oIterator2.next(); 
       String sParentKey = sChildKey.substring(0,sChildKey.indexOf("_")); 

       if (sKey.equals(sParentKey)) 
       { 
        oChildMap.put(sChildKey, coMap.get(sChildKey)); 
       } 
      } 

      oClassified.put(sKey, oChildMap); 
     } 

     return oClassified; 
    } 

} 
+0

是的關鍵是獨一無二的,我的問題是我想分組得到1_x並將它們放在一個單獨的hasmap(使關鍵唯一)或arraylist,我也做了這個拆分,但缺乏類似的值分組。 – SAR

+1

然後你的問題就解決了。在分割和檢查鍵值時獲得相似的鍵時,您現在可以將它們分組在不同的地圖中。當您使用「_」字符分割字符串時,檢查會出現。如果數組[0]相似,則將其放在不同的地圖中。我有回答你的問題嗎? – Oneb

+0

謝謝,但我很抱歉地說,我無法讓你分享代碼。你作爲答案給出的答案我遵循同樣的事情,但我真的不知道下一部分比較和分組到不同的地圖。請 – SAR

2

從澄清評論我假設你想過濾你的鑰匙:只包含開始1_,另一個與10_

使用普通的Java鍵一個地圖,可以通過使用Map<String, Map<String, String>>並遍歷原始地圖做到這一點:

Map<String, Map<String, String>> filtered = new HashMap<String, Map<String, String>>(); 
for (Entry<String, String> sourceEntry : source.entrySet()) { 
    String keyPart = sourceEntry.getKey().split("_")[0]; 
    Map<String, String> filteredTarget = filtered.get(keyPart); 
    if (filteredTarget == null) { 
    filteredTarget = new HashMap<String, String>(); 
    filtered.put(keyPart, filteredTarget); 
    } 
    filteredTarget.put(sourceEntry.getKey(), sourceEntry.getValue()); 
} 

Map<String, String> oneMap = filtered.get("1"); 
assert oneMap.get("1_19").equals("30/04/2012"); 
assert filtered.get("10").get("10_37").equals("27/04/2012"); 

請注意在源地圖或地圖過濾的後續變化不會更新其他地圖。如果你想有這樣的事情,比Guava可以幫助:

Map<String, Map<String, String>> filtered = Maps.newHashMap(); 
for (Entry<String, String> sourceEntry : source.entrySet()) { 
    final String keyPart = sourceEntry.getKey().split("_")[0]; 
    Map<String, String> filteredTarget = filtered.get(keyPart); 
    if (filteredTarget == null) { 
    filteredTarget = Maps.filterKeys(source, new Predicate<String>() { 

     @Override 
     public boolean apply(String input) { 
     return input.startsWith(keyPart + "_"); 
     } 

    }); 
    filtered.put(keyPart, filteredTarget); 
    } 
} 

Map<String, String> oneMap = filtered.get("1"); 
assert oneMap.get("1_19").equals("30/04/2012"); 
assert filtered.get("10").get("10_37").equals("27/04/2012"); 

oneMap.put("1_50", "Test"); 
assert source.get("1_50").equals("Test"); 
+0

請看看我的編輯 – SAR

2

你想創建地圖,其中對外部映射的關鍵是給定的按鍵的第一數量的Map地圖。以下代碼未經測試,但您可以將其作爲提示...

Map<String, Date> givenMap; // this is the given Map 
Map<Integer, Map<Integer, Date> > newMap = new HashMap<Integer, Map<Integer, Date> >(); 
for (Map.Entry<String, Date> entry : givenMap.entrySet()) { 
    String givenKey = entry.getKey(); 
    Date givenDate = entry.getValue(); 
    String[] splittedKey = givenKey.split("_"); 
    int newOuterKey = Integer.parseInt(splittedKey[0]); 
    int newInnerKey = Integer.parseInt(splittedKey[1]); 
    if (!newMap.containsKey(newOuterKey)) { 
     newMap.put(newOuterKey, new HashMap<Integer, Date>(); 
    } 
    newMap.get(newOuterKey).put(newInnerKey, givenDate); 
} 
+0

請看看我的編輯。 – SAR

相關問題