标签:string java语言 turn return aaaaa col hlist 排序 ble
1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序
package com.tao.test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; public class Sort { public static void main(String[] args) { // TreeMap排序1 Map<String, String> treeMap = new TreeMap<String, String>(new Comparator<String>() { public int compare(String o1, String o2) { // 升序排序 return o1.compareTo(o2); } }); treeMap.put("c", "ccccc"); treeMap.put("a", "aaaaa"); treeMap.put("b", "bbbbb"); treeMap.put("d", "ddddd"); // 排序后输出 for (String key : treeMap.keySet()) { System.out.println("Key=" + key + ", Value=" + treeMap.get(key)); } // TreeMap排序2 Map<String, Integer> treeMap2 = new TreeMap<String, Integer>(); treeMap2.put("d", 3); treeMap2.put("b", 4); treeMap2.put("a", 7); treeMap2.put("c", 1); // 转换成list List<Map.Entry<String, Integer>> treeList = new ArrayList<Map.Entry<String, Integer>>(treeMap2.entrySet()); Collections.sort(treeList, new Comparator<Map.Entry<String, Integer>>() { // 升序排序 public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); // 排序后输出 for (Map.Entry<String, Integer> m : treeList) { System.out.println("Key=" + m.getKey() + ", Value=" + m.getValue()); } // HashMap排序 Map<String, Integer> hashMap = new HashMap<String, Integer>(); hashMap.put("c", 3); hashMap.put("a", 2); hashMap.put("b", 1); hashMap.put("d", 4); List<Map.Entry<String, Integer>> hashList = new ArrayList<Map.Entry<String, Integer>>(hashMap.entrySet()); Collections.sort(hashList, new Comparator<Map.Entry<String, Integer>>() { // 升序排序 public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); // 排序后输出 for (Map.Entry<String, Integer> m : hashList) { System.out.println("Key=" + m.getKey() + ", Value=" + m.getValue()); } // List<Integer>排序 List<Integer> nums = new ArrayList<Integer>(); nums.add(3); nums.add(5); nums.add(2); nums.add(1); // 升序排序(默认) Collections.sort(nums); // 排序后输出 System.out.println(nums); // List<Bean>排序 List<User> users = new ArrayList<User>(); users.add(new User(2, "jack")); users.add(new User(1, "tom")); users.add(new User(3, "keck")); users.add(new User(4, "tao")); // id升序排序 Collections.sort(users); // 排序后输出 for (User user : users) { System.out.println(user.getId() + "," + user.getName()); } // List<Map>排序 List<Map<String, Integer>> listMap = new ArrayList<Map<String, Integer>>(); Map<String, Integer> map = new HashMap<>(); map.put("age", 20); map.put("sex", 1); listMap.add(map); Map<String, Integer> map2 = new HashMap<>(); map2.put("age", 29); map2.put("sex", 2); listMap.add(map2); Map<String, Integer> map3 = new HashMap<>(); map3.put("age", 35); map3.put("sex", 1); listMap.add(map3); // 按照map值排序 Collections.sort(listMap, new Comparator<Map<String, Integer>>() { @Override public int compare(Map<String, Integer> o1, Map<String, Integer> o2) { return o1.get("age").compareTo(o2.get("age"));// age升序排序 } }); // 排序后输出 for (Map<String, Integer> m : listMap) { System.out.println(m); } } }
2.List<User>排序的User.java类:
package com.tao.test; public class User implements Comparable<User>{ private int id; private String name; public User(int id, String name) { super(); this.id = id; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public int compareTo(User o) { return this.id - o.getId();//id升序排序 } }
3.运行截图
Java语言利用Collections.sort对Map,List排序
标签:string java语言 turn return aaaaa col hlist 排序 ble
原文地址:https://www.cnblogs.com/i-tao/p/9105163.html