Map的排序Demo

来源:互联网 发布:c语言的头文件怎么写 编辑:程序博客网 时间:2024/05/21 15:36
已知一个HashMap<Integer,Person>集合, Person有name(String)和age(int)属性。请写一个方法实现对HashMap的排序功能。该方法接收HashMap<Integer,Person>为形参,返回类型为HashMap<Integer,Person>,要求对HashMap中的Person的age升序进行排序。排序时key=value键值对不得拆散。public class SortedHashMapDemo {  public static void main(String[] args) {  HashMap<Integer, Person> map = new LinkedHashMap<Integer, Person>();  map.put(0, new Person("小明", 20));  map.put(1, new Person("小二", 26));  map.put(2, new Person("小四", 19));  map.put(3, new Person("阿七", 33));  map.put(4, new Person("十四", 25));  map.put(4, new Person("小花", 19));  System.out.println(map);  HashMap<Integer, Person> sortedHashMap = SortedHashMap(map);  System.out.println(sortedHashMap);  }  public static HashMap<Integer, Person> SortedHashMap(  HashMap<Integer, Person> map) {  // 获得键值对Set集合  Set<Entry<Integer, Person>> entrySet = map.entrySet();  // 将键值对Set集合转化为List以用Collections来排序  List<Entry<Integer, Person>> list = new ArrayList<Map.Entry<Integer, Person>>(  entrySet);  // 通过Collections来排序,添加比较器,比较年龄  Collections.sort(list, new Comparator<Entry<Integer, Person>>() {  @Override  public int compare(Entry<Integer, Person> o1, Entry<Integer, Person> o2) {  int result = o2.getValue().age - o1.getValue().age;  result = result == 0 ? o2.hashCode() - o1.hashCode() : result;  return result;  }  });  // 创建LinkedHashMap来存储排好序的List元素  LinkedHashMap<Integer, Person> linkedHashMap = new LinkedHashMap<Integer, Person>();  // 遍历List,将元素添加到linkedHashMap中  for (Entry<Integer, Person> entry : list) {  linkedHashMap.put(entry.getKey(), entry.getValue());  }  return linkedHashMap;  }}class Person {  String name;  int age;  public Person(String name, int age) {  super();  this.name = name;  this.age = age;  }  @Override  public String toString() {  return "Person [name=" + name + ", age=" + age + "]";  }}TreeMap排序①根据其键的自然顺序进行排序(自定义对象须实现Comparable接口并重写compare方法)②根据创建映射时提供的Comparator进行排序,具体取决于使用的构造方法。public class TreeMapReview {  public static void main(String[] args) {  test1();  test2();  }  /**  * 自定义对象做key,需满足以下两个条件之一,否则抛出异常java.lang.ClassCastException:com.yu.bean.Info cannot be cast to java.lang.Comparable  * ①实现Comparable接口并重写compare方法  * ②构造TreeMap对象时,需传入Comparator  * 当两者都有,以Comparator来排序  */  private static void test2() {  TreeMap<Info, String> map=new TreeMap<Info, String>(new Comparator<Info>() {  @Override  public int compare(Info o1, Info o2) {  int num = o2.getId() - o1.getId();  num = num == 0 ? o2.getAdress().hashCode() - o1.getAdress().hashCode() : num;  return num;  }  });  map.put(new Info(000, "hhh"), "qqq");  map.put(new Info(001, "hhh"), "aaa");  map.put(new Info(002, "hhh"), "zzz");  map.put(new Info(000, "hhh"), "qqq");  System.out.println(map);  }  /**  * String类型或基本类型包装类做key  * String类实现了Comparable接口,可以进行自然排序  */  private static void test1() {  TreeMap<String, String> map = new TreeMap<String, String>();  map.put("a", "111");  map.put("b", "123");  map.put("c", "121");  map.put("c", "121");  Set<Entry<String, String>> entrySet = map.entrySet();  for (Entry<String, String> entry : entrySet) {  System.out.println("key=" + entry.getKey() + " value="  + entry.getValue());  }  // output:// key=a value=111// key=b value=123// key=c value=121  }}
原创粉丝点击