数据去重,排序


看到array,就要想到角标。


看到link,就要想到first,last。


看到hash,就要想到hashCode,equals.


看到tree,就要想到两个接口。Comparable,Comparator。

我们在开发的过程中使用HashMap比较多,在Map中在Map 中插入、删除和定位元素,HashMap 是最好的选择。

但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。

如果需要输出的顺序和输入的相同,那么用LinkedHashMap 可以实现,它还可以按读取顺序来排列.

LinkedHashMap唯一,存储取出有序


TreeMap根据key默认排序

 

Set:注重独一无二的性质,该体系集合可以知道某物是否已近存在于集合中,不会存储重复的元素


用于存储无序(存入和取出的顺序不一定相同)元素,值不能重复。



Map对key重复的数据去重
Map<String, String> map = new HashMap<String, String>(); 2 map.put("1", "hello1"); 3 map.put("2", "hello2"); 4 map.put("3", "hello3"); 5 map.put("4", "hello4"); 6 map.put("3", "hello3"); 7 map.put("4", "hello4"); 8 map.put("3", "hello3"); 9 map.put("4", "hello4"); 10 System.out.println(map+"===");

Map<String, String> map2 = new HashMap<String, String>();
13         for (String m : map.keySet()) {
14             if(map2.containsKey(m)){
15                 map2.remove(m);
16             }
17             map2.put(m, map.get(m));
18         }
19         System.out.println(map2);
20     }
 
//去除数组中重复的记录    
public static String[] array_unique(String[] a) {    
    // array_unique    
    List<String> list = new LinkedList<String>();    
    for(int i = 0; i < a.length; i++) {    
        if(!list.contains(a[i])) {    
            list.add(a[i]);    
        }    
    }    
    return (String[])list.toArray(new String[list.size()]);    
}  


       




原文地址:https://www.cnblogs.com/lhl-shubiao/p/8506954.html