2013-10-08 25 views
0

類似的項目我有這樣分組使用HashMap的

0 [0.327097, 0.326998, 0.0] 
0 [1.056364, 0.601873, 0.0] 
0 [1.273154, 1.656441, 0.0] 
1 [1.48469, 0.095074, 0.0] 
1 [1.061504, -0.768175, 1.0] 

一個輸入,我需要給他們排序爲

0 : [ [0.327097, 0.326998, 0.0] ,[1.056364, 0.601873, 0.0], [1.273154, 1.656441, 0.0]] 
1 : [ [1.48469, 0.095074, 0.0], [1.061504, -0.768175, 1.0]] 

我不喜歡這個..

,但我沒有得到相同的output.my輸出重複。

u能請幫助我......

Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>(); 
        String[] subparts = finalline.split("\\[");  

        String groupKey; 
        String value; 

        if (subparts.length == 1) {         
         groupKey = null; 
         value = subparts[0]; 
        } 
        else if (subparts.length == 2) {       
         groupKey = subparts[0]; 
         value = subparts[1]; 
        } 
         else { 
         throw new IllegalArgumentException("Can not parse string"); 
        } 

        Collection<String> groupContents = groupMap.get(groupKey);  
        if (groupContents == null) {          
         groupMap.put(groupKey, groupContents = new ArrayList<String>()); 
        } 
        groupContents.add(value);          

       } 

回答

2

groupMap地圖的價值是另外一個集合,這樣你就可以通過外環內的訪問集合下面

Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>(); 
for(String key : groupMap.keySet()){ 
    System.out.println("Key: " + key); 
    Collection<String> values = groupMap.get(key); 
    for(String value : values){ 
     System.out.println("value: " + value); 
    } 
} 
1
給出
Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>(); 
for (String s : groupMap.keySet()) { 
    for (String s1 : groupMap.get(s)) { 
     System.out.println(s1); 
    } 
} 

集合中的集合僅僅意味着嵌套循環 - 就像2D數組一樣。

1

最有效的方法循環遍歷您Map項如下:

Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>(); 
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) { 
    System.out.println("Key: "+entry.getKey()); 
    for (String val : values) { 
     System.out.printlnln("Value: "+entry.getValue()); 
    } 
} 
1

我建議使用由「番石榴」,而不是一個HashMultimap

它有助於輕鬆處理從鍵到多個值的映射,並且是將鍵與任意多個值關聯的一般方法。

下面是一個例子。

Multimap<String, String> map = HashMultimap.create(); 
map.put("1", "a"); 
map.put("1", "b"); 

map.put("2", "c"); 
map.put("2", "d"); 

現在,您可以使用「values()」視圖遍歷地圖中的值。

for(String value : map.values()) { 
    System.out.println(value); 
} 

這會給你

a 
b 
c 
d 

或者,如果你想要的鍵和值一起,那麼你可以使用「項()」的說法。

for(Map.Entry<String, String> entry : map.entries()) { 
    System.out.println("Key: " + entry.getKey() + " Value : " + entry.getValue()); 
} 

這會給你

Key: 1 Value : a 
Key: 1 Value : b 
Key: 2 Value : c 
Key: 2 Value : d 

如果你正在尋找一個普通的舊式Java簡單的解決方案

Map<String, List<String>> map = new HashMap<String, List<String>>(); 
// ... Some code to put values in the map 
for(String key : map.keySet()){ 
    System.out.println("\nKey: " + key); 
    List<String> values = map.get(key); 
    for(String value : values) { 
     System.out.println("Value: " + value); 
    } 
} 
1

最好的和最有效的方式來遍歷地圖的條目是:

Map<String, Collection<String>> map; 
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) { 
    System.out.print(entry.getKey()+":"); 
    for (String str : entry.getValue()) 
     System.out.println(str); 
} 

此代碼將生成您請求的輸出。

請注意,在任何時候都沒有鑰匙擡頭。當迭代條目集時,您可以直接訪問(鍵入)鍵和(鍵入)值。