删除Linkedhashmap中的重复字符串

时间:2014-09-01 09:34:07

标签: java string collections

我有一个链接的哈希映射

LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap();

其键集和值为

50,Tam,medicine = 7
50,Pep,surgery = 3
50,Tam,surgery = 6 
50,Ery,treatment = 2
60,cap,treatment = 3
60,syn,treatment = 5
60,syn,surgery = 6
60,pep,surgery = 5
60,pep,medicine = 3

我需要根据第一个字符串组合它,即str [0]。然后删除str [1]或str [2]重复并具有最高值的键集。有些事情比如按升序排列并删除其str [1]或str [2]重复并具有最高值的键集。

最终输出应为

50,Ery,treatment=2
50,Pep,surgery=3
50,Tam,surgery=6 //should be deleted as in group 50 "surgery" is repeated above
50,Tam,medicine=7
60,cap,treatment=3
60,pep,medicine=3
60,pep,surgery=5 //should be deleted as in group 60 "pep" is repeated above 
60,syn,treatment=5 //should be deleted as in group 60 "treatment" is repeated above
60,syn,surgery=6 

public static LinkedHashMap sortHashMapByValuesD(Map passedMap) {
        List mapKeys = new ArrayList(passedMap.keySet());
        List mapValues = new ArrayList(passedMap.values());
        Map<String, Integer> dUnique = new LinkedHashMap<>();
        Collections.sort(mapValues);
        Collections.sort(mapKeys);

        LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap();

        Iterator valueIt = mapValues.iterator();
        while (valueIt.hasNext()) {
            Object val = valueIt.next();
            Iterator keyIt = mapKeys.iterator();

            while (keyIt.hasNext()) {
                Object key = keyIt.next();
                String comp1 = passedMap.get(key).toString();
                String comp2 = val.toString();

                if (comp1.equals(comp2)) {
                    passedMap.remove(key);
                    mapKeys.remove(key);
                    sortedMap.put((String) key, (Integer) val);
                    break;
                }

            }

        }
        for (String sorted : sortedMap.keySet()) {
            String[] dsort = sorted.split(",");
           // System.out.println(sorted + sortedMap.get(sorted));


            if (dUnique.get(dsort[0]) == null) {
                dUnique.put(sorted, sortedMap.get(sorted));
                System.out.println(dUnique.keySet());
            } else {
                for (int i = 1; i < dsort.length; i++) {
                    if (dUnique.get(dsort[i]) == null) {
                        dUnique.put(sorted, sortedMap.get(sorted));
                    } else {
                        dUnique.remove(sorted);
                    }
                }
            }
        }
        System.out.println(dUnique.keySet());
        return sortedMap;
    }

1 个答案:

答案 0 :(得分:1)

此代码可以帮助您:

public static LinkedHashMap<String, Integer> sortHashMapByValuesD(LinkedHashMap passedMap) {
    LinkedHashMap  temp = passedMap;
    LinkedHashMap<String, Integer> dUnique = new LinkedHashMap();
    Iterator it=temp.keySet().iterator();
    Map hp = new HashMap();
    while(it.hasNext()){
        String key = (String)it.next();
        String preKey =   key.substring(0,key.lastIndexOf(",") );
        if( !hp.containsKey(preKey) ){
            dUnique.put(key, (Integer)temp.get(key));
        }
        hp.put(preKey,preKey);
    }

    return dUnique;

}
祝你好运。