这篇来学习LinkedHashMap和TreeMap基本使用,先来看看LinkedHashMap,在前面学习Set集合的时候,我们介绍了一个LinkedHashSet的类,这个也一样,算法实现是采用链表实现,属于HashMap家族。TreeMap是Map集合的另外一种实现方式,底层是二叉树实现。这篇,我们先来一个基本了解和使用就好。
1.LinkedHashMap
LinkedHashMap有一个好处就是,如何存储顺序就如何读取出来,看看下面这个简单例子。
package map; import java.util.LinkedHashMap; public class Demo3_LinkedHashMap { public static void main(String[] args) { LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>(); lhm.put("张三", 23); lhm.put("王五", 25); lhm.put("李四", 24); lhm.put("赵六", 26); System.out.println(lhm); } }
运行结果:
{张三=23, 王五=25, 李四=24, 赵六=26}
2.TreeMap
前面我们介绍都是HashMap来举例,现在开始介绍TreeMap,看名称,底层是二叉树算法实现的,说明支持元素排序。在使用TreeMap比较自定义对象作为键的情况,需要自定义对象实现Comparable接口是重写Comparator方法。
package bean; public class Student implements Comparable<Student> { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); 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 int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public int compareTo(Student s) { int num = this.age - s.age; //以年龄为主要比较条件 return num == 0? this.name.compareTo(s.name) : num; //如果年龄相等,再比较名称 } }
package map; import java.util.TreeMap; import bean.Student; public class Demo4_TreeMap { public static void main(String[] args) { TreeMap<Student, String> tm = new TreeMap<>(); tm.put(new Student("张三",23), "北京"); tm.put(new Student("李四",18), "上海"); tm.put(new Student("王五",31), "广州"); tm.put(new Student("赵六",43), "深圳"); tm.put(new Student("张三",22), "深圳"); System.out.println(tm); } }
运行结果,确实是按照年龄从小到大排序:
{Student [name=李四, age=18]=上海, Student [name=张三, age=22]=深圳, Student [name=张三, age=23]=北京, Student [name=王五, age=31]=广州, Student [name=赵六, age=43]=深圳}
当然,在TreeMap的时候,可以传入一个比较器来实现排序,下面这个比较器是,按照名称的unicode码表排序。
package map; import java.util.Comparator; import java.util.TreeMap; import bean.Student; public class Demo4_TreeMap { public static void main(String[] args) { TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num = s1.getName().compareTo(s2.getName()); return num == 0? s1.getAge() - s2.getAge() : num; } }); tm.put(new Student("张三",23), "北京"); tm.put(new Student("李四",18), "上海"); tm.put(new Student("王五",31), "广州"); tm.put(new Student("赵六",43), "深圳"); tm.put(new Student("张三",22), "深圳"); System.out.println(tm); } }
运行结果:
{Student [name=张三, age=22]=深圳, Student [name=张三, age=23]=北京, Student [name=李四, age=18]=上海, Student [name=王五, age=31]=广州, Student [name=赵六, age=43]=深圳}