java集合框架非常好的封装了对数据基本处理的底层实现细节,可以直接调用API完成对数据的基本的操作,它们之间的关系就不累述了,这里主要想谈下LinkedHashMap与TreeMap的排序问题。
System.out.println("顺序put进linkedHashMap");
LinkedHashMap<Integer,String> map = new LinkedHashMap<Integer,String>();
for(int i = 0;i < 10;i ++){
map.put(i, "test" + i);
System.out.println("put: key :"+i+" value: test" + i);
}
System.out.println("key转换为Set去遍历");
for(Integer i: map.keySet()){
System.out.println("key : "+i+" value : "+map.get(i));
}
Map<String,String> map = new LinkedHashMap<String,String>();
for(int i = 1;i <= 10;i ++){
map.put(String.valueOf(i), String.valueOf(i + 1));
}
//遍历
for(String str : map.keySet()){
System.out.println("key ===>"+str+", value ====>"+map.get(str));
}
运行结果:
顺序put进linkedHashMap
put: key :0 value: test0
put: key :1 value: test1
put: key :2 value: test2
put: key :3 value: test3
put: key :4 value: test4
put: key :5 value: test5
put: key :6 value: test6
put: key :7 value: test7
put: key :8 value: test8
put: key :9 value: test9
key转换为Set去遍历
key : 0 value : test0
key : 1 value : test1
key : 2 value : test2
key : 3 value : test3
key : 4 value : test4
key : 5 value : test5
key : 6 value : test6
key : 7 value : test7
key : 8 value : test8
key : 9 value : test9
Map<Integer, Integer> treeMap = new TreeMap<Integer, Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if(o1 > o2)
return 1;
else if(o1 == o2)
return 0;
else
return -1;
}
});
//随机put
for(int i = 0;i < 10; i ++){
Random r = new Random();
Integer key = r.nextInt(10);
Integer value = r.nextInt(20);
treeMap.put(key, value);
System.out.println("key --->"+key+",value --->"+value);
}
System.out.println("遍历结果如下");
for(int i : treeMap.keySet()){
System.out.println("key --->" + i + ", value --->"+treeMap.get(i));
}
结果:
key —>1,value —>0
key —>9,value —>12
key —>7,value —>0
key —>3,value —>14
key —>6,value —>3
key —>0,value —>11
key —>1,value —>15
key —>8,value —>0
key —>0,value —>7
key —>2,value —>1
遍历结果如下
key —>0, value —>7
key —>1, value —>15
key —>2, value —>1
key —>3, value —>14
key —>6, value —>3
key —>7, value —>0
key —>8, value —>0
key —>9, value —>12
最开始比较器里的只实现了一个if判断,之后排序后一直得不到值,后来查找相关资料才知道一定要有两个if判断才行。
Map<Integer, Integer> treeMap = new TreeMap<Integer, Integer>();
//随机put
for(int i = 0;i < 10; i ++){
Random r = new Random();
Integer key = r.nextInt(10);
Integer value = r.nextInt(20);
treeMap.put(key, value);
System.out.println("key --->"+key+",value --->"+value);
}
List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(treeMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
if(o1.getValue() > o2.getValue())
return 1;
else if(o1.getValue() == o2.getValue())
return 0;
else
return -1;
}
});
System.out.println("遍历结果如下");
for(Map.Entry<Integer, Integer> entry : list){
System.out.println("key--->"+entry.getKey()+",value --->"+entry.getValue());
}
结果:
key —>6,value —>8
key —>3,value —>14
key —>1,value —>11
key —>1,value —>4
key —>2,value —>8
key —>6,value —>2
key —>0,value —>17
key —>0,value —>19
key —>8,value —>18
key —>1,value —>10
遍历结果如下
key—>6,value —>2
key—>2,value —>8
key—>1,value —>10
key—>3,value —>14
key—>8,value —>18
key—>0,value —>19
注:这里遍历出的结果比put进去的节点要少,因为key是唯一的,后put进去的key会覆盖掉给entry.
其实同理我们也可以为Map按key排序:
Map<Integer, Integer> treeMap = new TreeMap<Integer, Integer>();
//随机put
for(int i = 0;i < 10; i ++){
Random r = new Random();
Integer key = r.nextInt(10);
Integer value = r.nextInt(20);
treeMap.put(key, value);
System.out.println("key --->"+key+",value --->"+value);
}
List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(treeMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
if(o1.getKey() > o2.getKey())
return 1;
else if(o1.getValue() == o2.getValue())
return 0;
else
return -1;
}
});
System.out.println("遍历结果如下");
for(Map.Entry<Integer, Integer> entry : list){
System.out.println("key--->"+entry.getKey()+",value --->"+entry.getValue());
}
结果:
key —>9,value —>15
key —>5,value —>0
key —>5,value —>4
key —>6,value —>17
key —>4,value —>3
key —>6,value —>0
key —>1,value —>3
key —>3,value —>3
key —>1,value —>2
key —>1,value —>19
遍历结果如下
key—>1,value —>19
key—>3,value —>3
key—>4,value —>3
key—>5,value —>4
key—>6,value —>0
key—>9,value —>15