LinkedHashMap:键的数据结构是哈希表和链表,键唯一,而且有序, 哈希表保证唯一,链表保证有序;
LinkedHashMap<Integer, String> map = new LinkedHashMap<>(); //自动根据键排序 map.put(10,"aaa"); map.put(20, "aaa"); map.put(30, "aaa"); map.put(40, "aaa"); map.put(50, "aaa"); map.put(60, "aaa"); Set<Integer> integers = map.keySet(); for (Integer integer : integers) { String value = map.get(integer); System.out.println(integer+"==="+value); }TreeMap:键的数据结构是二叉树,可以对键进行排序,排序:自然排序,比较器排序。
TreeMap<Integer, String> map = new TreeMap<>(); //自动根据键值排序; map.put(10, "aaa"); map.put(200, "aaa"); map.put(30, "aaa"); map.put(4330, "aaa"); map.put(150, "aaa"); map.put(160, "aaa"); Set<Integer> integers = map.keySet(); for (Integer integer : integers) { String value = map.get(integer); System.out.println(integer + "===" + value); }TreeMap之自然排序:要求元素实现Comparable接口,重写compareTo方法;
代码如下:
public class MyTest2 { public static void main(String[] args) { TreeMap<Student, String> map = new TreeMap<>(); map.put(new Student("张三", 23333), "s001"); map.put(new Student("李四", 24), "s002"); map.put(new Student("王五", 25), "s003"); map.put(new Student("赵六", 26), "s004"); map.put(new Student("张三", 23), "s005"); Set<Map.Entry<Student, String>> entries = map.entrySet(); for (Map.Entry<Student, String> en : entries) { System.out.println(en); Student key = en.getKey(); String value = en.getValue(); System.out.println(key+"===="+value); } } } //student类: package org.westos.demo4; import java.util.Objects; public class Student implements Comparable<Student>{ private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public int compareTo(Student o) { int i = this.age - o.age; int j=i==0?this.name.compareTo(o.name):i; return j; } }比较器排序:传入比较器排序:
学生类 public class Student{ private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } } 测试类: public class MyTest3 { public static void main(String[] args) { TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() { @Override //比较器根据姓名长度排序,但是同时要比较姓名内容,和年龄,防止录入失败; public int compare(Student s1, Student s2) { int i = s1.getName().length() - s2.getName().length(); int j=i==0?s1.getName().compareTo(s2.getName()):i; int h=j==0?s1.getAge()-s2.getAge():j; return h; } }); map.put(new Student("张三aaa", 23333), "s001"); map.put(new Student("李四bbbbbb", 24), "s002"); map.put(new Student("王五", 25), "s003"); map.put(new Student("赵六ddfdfd", 26), "s004"); map.put(new Student("张三", 23), "s005"); Set<Map.Entry<Student, String>> entries = map.entrySet(); for (Map.Entry<Student, String> en : entries) { Student key = en.getKey(); String value = en.getValue(); System.out.println(key + "====" + value); } } }