Map 排序
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp42
[size=12px;][size=12px;][size=12px;]Map的排序常分为两种情况,1、按[/size]key[size=12px;]值排序;2、按value排序[/size][/size][/size]
[size=12px; font-family: 宋体;] 1.[size=12px; font-family: 宋体;]我们先讨论按[/size]key[size=12px; font-family: 宋体;]值进行排序[/size]
[size=12px; font-family: 宋体;]我们先看一下这个[/size]Sorter[size=12px; font-family: 宋体;]类:[/size][/size]
[size=12px; font-family: 宋体;] public static Map sort(Map map) {
Map mapVK = new TreeMap(
new Comparator() {
public int compare(Object obj1, Object obj2) {
String v1 = (String)obj1;
String v2 = (String)obj2;
int s = v2.compareTo(v1);
return s;
}
}
);[/size]
[size=12px; font-family: 宋体;] Set col = map.keySet();
Iterator iter = col.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
Integer value = (Integer) map.get(key);
mapVK.put(key, value);
}
return mapVK;
}
}[/size]
public class SortHashMap {
[size=12px; font-family: 宋体;] }[/size]
[size=12px; font-family: 宋体;] public static void main(String[] args) {
Map maps = new HashMap();
maps.put("boy",;
maps.put("cat", 7);
maps.put("dog", 1);
maps.put("apple", 5);
//排序前的输出[/size]
Set set = maps.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry entry1=(Map.Entry)i.next();
System.out.println(entry1.getKey() + "-------->" + entry1.getValue());
}
[size=12px; font-family: 宋体;] System.out.println("----------------");
//排序后的输出[/size]
Map sortMaps = Sorter.sort(maps);
Set sortSet = sortMaps.entrySet();
Iterator ii = sortSet.iterator();
while(ii.hasNext()){
Map.Entry entry1=(Map.Entry)ii.next();
System.out.println(entry1.getKey() + "-------->" + entry1.getValue());
}
}
}
cat-------->7
apple-------->5
dog-------->1
boy-------->8
dog-------->1
cat-------->7
boy-------->8
apple-------->5
经过排序后的Map有序了,是按照字母的逆序排列的。
还是上面的那个例子,我们想要按照各类对象的数量打印出类别的名称。
我们再来修改一下这个SortHashMap类:
[size=12px; font-family: 宋体;] public SortHashMap() {[/size]
[size=12px; font-family: 宋体;] public static void main(String[] args) {
Map maps = new HashMap();
maps.put("boy",;
maps.put("cat", 7);
maps.put("dog", 1);
maps.put("apple", 5);
//排序前的输出[/size]
Set set = maps.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry entry1=(Map.Entry)i.next();
System.out.println(entry1.getKey() + "-------->" + entry1.getValue());
}
[size=12px; font-family: 宋体;] System.out.println("----------------");
//排序后的输出[/size]
List> info = new ArrayList>(maps.entrySet());
Collections.sort(info, new Comparator>() {
public int compare(Map.Entry obj1, Map.Entry obj2) {
return obj2.getValue() - obj1.getValue();
}
});
[size=12px; font-family: 宋体;] for (int j = 0; j" + info.get(j).getValue());
}
}
}[/size]
cat-------->7
apple-------->5
dog-------->1
boy-------->8
boy------->8
cat------->7
apple------->5
dog------->1
[size=12px; font-family: 宋体;] 1.[size=12px; font-family: 宋体;]我们先讨论按[/size]key[size=12px; font-family: 宋体;]值进行排序[/size]
[size=12px; font-family: 宋体;]我们先看一下这个[/size]Sorter[size=12px; font-family: 宋体;]类:[/size][/size]
[size=12px; font-family: 宋体;] public static Map sort(Map map) {
Map mapVK = new TreeMap(
new Comparator() {
public int compare(Object obj1, Object obj2) {
String v1 = (String)obj1;
String v2 = (String)obj2;
int s = v2.compareTo(v1);
return s;
}
}
);[/size]
[size=12px; font-family: 宋体;] Set col = map.keySet();
Iterator iter = col.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
Integer value = (Integer) map.get(key);
mapVK.put(key, value);
}
return mapVK;
}
}[/size]
public class SortHashMap {
[size=12px; font-family: 宋体;] }[/size]
[size=12px; font-family: 宋体;] public static void main(String[] args) {
Map maps = new HashMap();
maps.put("boy",;
maps.put("cat", 7);
maps.put("dog", 1);
maps.put("apple", 5);
//排序前的输出[/size]
Set set = maps.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry entry1=(Map.Entry)i.next();
System.out.println(entry1.getKey() + "-------->" + entry1.getValue());
}
[size=12px; font-family: 宋体;] System.out.println("----------------");
//排序后的输出[/size]
Map sortMaps = Sorter.sort(maps);
Set sortSet = sortMaps.entrySet();
Iterator ii = sortSet.iterator();
while(ii.hasNext()){
Map.Entry entry1=(Map.Entry)ii.next();
System.out.println(entry1.getKey() + "-------->" + entry1.getValue());
}
}
}
cat-------->7
apple-------->5
dog-------->1
boy-------->8
dog-------->1
cat-------->7
boy-------->8
apple-------->5
经过排序后的Map有序了,是按照字母的逆序排列的。
还是上面的那个例子,我们想要按照各类对象的数量打印出类别的名称。
我们再来修改一下这个SortHashMap类:
[size=12px; font-family: 宋体;] public SortHashMap() {[/size]
[size=12px; font-family: 宋体;] public static void main(String[] args) {
Map maps = new HashMap();
maps.put("boy",;
maps.put("cat", 7);
maps.put("dog", 1);
maps.put("apple", 5);
//排序前的输出[/size]
Set set = maps.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry entry1=(Map.Entry)i.next();
System.out.println(entry1.getKey() + "-------->" + entry1.getValue());
}
[size=12px; font-family: 宋体;] System.out.println("----------------");
//排序后的输出[/size]
List> info = new ArrayList>(maps.entrySet());
Collections.sort(info, new Comparator>() {
public int compare(Map.Entry obj1, Map.Entry obj2) {
return obj2.getValue() - obj1.getValue();
}
});
[size=12px; font-family: 宋体;] for (int j = 0; j" + info.get(j).getValue());
}
}
}[/size]
cat-------->7
apple-------->5
dog-------->1
boy-------->8
boy------->8
cat------->7
apple------->5
dog------->1
博客已转移至 http://blog.yemou.net/