2016-09-22 55 views
0

目前我已經在values字段中對我的Hashmap進行了排序。 我需要計算與散列映射中每個值關聯的條目數。用於計算java中hashmap中每個值對應的條目數的邏輯

最初我想迭代排序後的hashmap並計算條目的數量,直到值沒有改變。爲了獲得下一個值,我需要進入下一個條目,但是直到一個循環迭代結束,沒有辦法做到這一點。 但我只是迷失在邏輯中,無法繼續。 :(

我試圖其使用過濾器流()的其他邏輯。 應用上爲1至50,然後計數其滿足謂詞中的條目中的值的濾波器。

for(int i = 1; i < COUNT; i++){ 
      int count = (int) single_count.entrySet().stream().filter(v -> v.getValue() == 1).count(); //collect(Collectors.toList()); 
      unigram_Nc.put(i, count); 
     } 

在這種情況下我知道散列表中的值,但我想知道一般解決方案,它返回與每個值對應的散列表中的條目數 是否有任何其他方法來計算具有特定值的條目數知道前面的數值嗎?

+1

提示:你需要創建一個新的'地圖<整數,整數>'那裏的新地圖,關鍵是從第一張地圖的價值,並在值新地圖是條目的計數。 –

回答

2

你可以用java 8 stream api來做這件事更容易。

對於這個你應該從你的地圖取值:map.values()

隨着.stream()你得到該集合流。

然後您可以使用collect方法與groupingBy收集器。

最後它可能是這個樣子:

final Map<Integer, Long> counts = map.values() // get the values 
    .stream()         // get the stream 
    .collect(
     Collectors.groupingBy(    // the result should be grouped 
      o -> o,       // the key of the result map is just the value 
      Collectors.counting()    // the value of result map is the count 
     ) 
    ); 
+0

謝謝。但是在這種情況下collect沒有指定Collectors.toMap()會如何返回地圖?另外,將結果轉換爲HashMap是否安全? –

+0

作爲['groupingBy']的JavaDoc(http://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#groupingBy-java.util.function.Function- java.util.stream.Collector-)函數說:「生成的收集器生成一個Map 。「 你不能簡單地從'Map'轉換爲'HashMap',但你可以通過使用適當的構造函數創建一個從'Map'一個'HashMap':'新的HashMap <>(計數)' –

+1

你可以得到'收藏家。 groupingBy(...)'通過使用三個參數版本:'HashMap counts = map.values()。stream().collect(Collectors.groupingBy(o - > O,HashMap中::新,Collectors.counting()))' – msandiford

0

對於較早的JDK,你可以指望這樣的:

Create a class and override its equals() and hashcode() method. Create a field of your preferred type say A and add another int type field to count. 
1) Your hashcode() method should return the hash value of your field A. 
2) In your equals() method, increase the value of count by 1 and set the count as value 

Now create 2 hashmaps, first will have your initial map's value as keys. The second one will have the result of all the counts of values. 

請參考下面的代碼片段:

class A 
{ 
    Integer count = 1; 
    String name; 

    @override 
    public int hashcode() 
    { 
     return name.hash(); 
    } 

    @override 
    public boolean equals(Object obj) 
    { 
     obj.count++; 
     secondmap.put(obj.name, obj.count);   

     return true; 
    } 

} 

Now in your main class: 

static firstmap = new ConcurrentMap<A, Integer>(); 

static secondmap = new ConcurrentMap<String, Integer>(); 

iterate over yourmap 
{ 
    put the value in firstmap as firstmap.put(yourmap value, 0); 
} 

在迭代結束時,您將擁有secondmap中的所有值。 注意:如果您的初始映射具有不同的簽名,那麼您可以通過A的構造函數明確設置String名稱的值。

這只是一個例子,根據你的解決方案,實際的實現可能會有所不同,但你可以參考這個邏輯。同時創建你的初始地圖,你可以實現這一點。這將爲您節省重複迭代的麻煩。

0

試試這個簡單的邏輯

Map<String,Integer> dataMap=new LinkedHashMap<String,Integer>(); //Your data map 
    Map<Integer,Integer> countMap=new LinkedHashMap<Integer,Integer>(); //map to count data map entries 

    //initializing with default value 
    dataMap.put("a", 1); 
    dataMap.put("b", 2); 
    dataMap.put("c", 1); 
    dataMap.put("d", 2); 
    dataMap.put("e", 1); 
    dataMap.put("f", 3); 
    dataMap.put("g", 1); 

    //main logic 
    dataMap.forEach((k,v) -> { 
     if(countMap.get(v)==null) 
      countMap.put(v, 0); 
     Integer count=countMap.get(v); 
     countMap.put(v,count+1); 
    }); 

    //printing the count 
    countMap.forEach((k,v) -> { 
     System.out.println(k+"  "+v); 
    }); 
+0

代碼只回答未在SO鼓勵。 –