文章标题
2016-10-10 23:03
309 查看
TreeMap的使用
今天学习的一点小知识 TreeMap好早以前学习的,今天别人问起来,才发现只记得可以排序,然后忘记了怎么排序,然后又去自己敲代码,然后总结了一下。TreeMap是JDK java.util.包中的一个类,是Map的一个子类,但这个类自带有排序功能,下面来说一说他的用法。
使用TreeMap对对象进行排序有两种方式 :
在new TreeMap的时候传入比较器。(比较器是自己写一个类,实现了Comparator接口,重写compare方法)
比较器:
/** * 比较器 */ class StudentCom implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); } }
实体类
package com.lfc.study.entity; /** * <p>Title: </p> * <p>Description: </p> * @author * @date: 2016年10月10日 下午9:10:56 * @version:1.0 */ public class 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 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; } }
执行代码:
package com.lfc.study; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import com.lfc.study.entity.Student; /** * <p>Title: </p> * <p>Description: </p> * @author lifacheng * @date: 2016年10月10日 下午9:06:01 * @version:1.0 */ public class MapTest { public static void main(String[] args) { Map<Student,Object> map = new HashMap<Student,Object>(); map.put(new Student("a",1), 1); map.put(new Student("a1",11), 1); map.put(new Student("a2",22), 1); map.put(new Student("a3",33), 1); map.put(new Student("a4",44), 1); Map<Student,Object> map1 = new TreeMap<Student, Object>(new StudentCom()); map1.putAll(map); for(Entry<Student, Object> entry: map1.entrySet()){ System.out.println(entry.getKey().getName()); } } }
我们可以看到执行结果:
第二种 让对象实现Comparable接口,重写里面的compareTo方法(也要重写对象里的equals和hashCode方法)。
实体类:
package com.lfc.study.entity; /** * <p>Title: </p> * <p>Description: </p> * @author lifacheng * @date: 2016年10月10日 下午9:10:56 * @version:1.0 */ 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 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 o) { return this.getAge() - o.getAge(); } }
执行代码:
package com.lfc.study; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import com.lfc.study.entity.Student; /** * <p>Title: </p> * <p>Description: </p> * @author lifacheng * @date: 2016年10月10日 下午9:06:01 * @version:1.0 */ public class MapTest { public static void main(String[] args) { Map<Student,Object> map = new HashMap<Student,Object>(); map.put(new Student("a",1), 1); map.put(new Student("a1",11), 1); map.put(new Student("a2",22), 1); map.put(new Student("a3",33), 1); map.put(new Student("a4",44), 1); Map<Student,Object> map1 = new TreeMap<Student, Object>(); map1.putAll(map); for(Entry<Student, Object> entry: map1.entrySet()){ System.out.println(entry.getKey().getName()); } } }
执行结果:
可以看到我们实现了同样的效果。
又涨姿势了。 gogogo!!!