2016-11-21 65 views
0

我已經創建了2個HashMaps,它們存儲來自兩個txt文件的字符串值。我能夠成功打印哪些值在兩個地圖之間重複,但是,我無法確定每個值的重複次數。確定兩個Hashmap中重複值的頻率

這裏是我的代碼來識別重複的值:

// find if hashmaps contain duplicate values 
    boolean val = wordsMap.keySet().containsAll(wordsMap1.keySet()); 

// create new entry for hashmap 
for (Entry<String, Integer> str : wordsMap.entrySet()) { 
System.out.println("================= " + str.getKey()); 


if(wordsMap1.containsKey(str.getKey())){ 
    System.out.println("Map2 Contains Map 1 Key"); 
    } 


} 

System.out.println("================= " + val); 

有什麼建議?謝謝

我沿線思維

Map.values().equals(Map1.values()){ 
count++; 

} 
+0

我能想到的唯一方法是創建重複鍵和重複值的計數,累加計之間的一個映射,每當重複發現 –

+0

你想要的是在這兩個地圖按鍵的數量,是那對嗎? – nandsito

回答

0

的東西如果我理解正確的話,你想找到這些都是常見的兩個地圖對象的值。 (添加null和/或其它檢查根據需要)

public static int duplicateCount(Map<String, Integer> m1, Map<String, Integer> m2) { 

    int count = 0; 
    Collection<Integer> m1Values = m1.values(); 
    Collection<Integer> m2Values = m2.values(); 

    for (Integer i : m1Values) { 
     if (m2Values.contains(i)) { 
      count++; 
     } 

    } 

    return count; 
} 
0

獲取地圖之間的重複的鍵的數目,

public static int getDuplicateKeyCount(Map<?, ?> m1, Map<?, ?> m2) { 
    Set<?> tempSet = new HashSet<>(m1.keySet()); 
    tempSet.retainAll(new ArrayList<>(m2.keySet())); 
    return tempSet.size(); 
} 

獲取地圖之間的重複值的數目,

public static int getDuplicateValueCount(Map<?, ?> m1, Map<?, ?> m2) { 
    List<?> tempList = new ArrayList<>(m1.values()); 
    tempList.retainAll(m2.values()); 
    return tempList.size();  
} 

獲取包含m1的值的頻率的地圖,以m2計

public static <K, V> Map<V, Integer> getValueFrequencyMap(Map<K, V> m1, Map<K, V> m2) { 
    Map<V, Integer> freq = new HashMap<>(); 

    Collection<V> col = m2.values(); 

    for(V val : m1.values()) { 
     freq.put(val, Collections.frequency(col, val)); 
    } 

    return freq; 
} 
0

以下是根據我對您疑問的理解編寫的示例程序。它會查找並顯示重複鍵以及重複值及其重複頻率。邏輯是:爲了找到頻率,將來自map1map2的重複元素作爲參數傳遞給countFrequency()方法,其依次將返回重複次數。

package com.rahul.stackoverflow; 

import java.util.HashMap; 
import java.util.Map; 
import java.util.Map.Entry; 

public class DuplicateValHashMap { 

    public static void main(String[] args) { 

     Map<String, Integer> map1 = new HashMap<String, Integer>(); 
     Map<String, Integer> map2 = new HashMap<String, Integer>(); 

     map1.put("A", 1); 
     map1.put("B", 2); 
     map1.put("C", 3); 
     map1.put("D", 4); 
     map1.put("E", 5); 

     map2.put("A", 1); 
     map2.put("F", 2); 
     map2.put("G", 1); 
     map2.put("H", 3); 
     map2.put("B", 2); 

     for(Entry<String, Integer> entrySet : map1.entrySet()){ 
      if(map2.containsKey(entrySet.getKey())){ 
       System.out.println("Map2 contains keys of map1."); 
       System.out.println("Duplicate keys are : " + entrySet.getKey()); 
      } 
      if(map2.containsValue(entrySet.getValue())){ 
       System.out.println("Map2 contains values of map1."); 
       System.out.println("Duplicate values are : " + entrySet.getValue()+ 
         " which is repeated " + countFrequency(map2, entrySet.getValue())+ " times."); 
      } 
     } 

    } 

    public static int countFrequency(Map<String, Integer> map, Integer value){ 
     int count = 0; 
     for(Entry<String, Integer> entrySet : map.entrySet()){ 
      if(value == entrySet.getValue()){ 
       count++; 
      } 
     } 

     return count; 

    } 

}