java treeMap 使用

来源:互联网 发布:支持windows的手机 编辑:程序博客网 时间:2024/06/16 13:18

下面的代码记录了TreeMap的简单使用方法,需要注意下面几点:

1、TreeMap不支持null值的key,支持null值的value;

2、TreeMap的构造函数没有指定comparator对象时,TreeMap要求key必须实现comparable接口,即对象可比较;

3、TreeMap同样无法在遍历对象的过程中调用remove函数删除元素,必须基于Iterator的方式删除元素,否则会抛出并发访问异常

package com.basic.util;import java.util.Comparator;import java.util.Iterator;import java.util.Map.Entry;import java.util.TreeMap;public class BasicTreeMap {public static void main(String[] args) {Student s1 = new Student("lpn", 27, 61.38);Student s2 = new Student("tbn", 28, 62.211);Student s3 = new Student("xyz", 29, 63.345);Student s4 = new Student("qwe", 30, 64.986);TreeMap<Student, Integer> tMap = new TreeMap<Student, Integer>(new StudentCmp());tMap.put(s1, 1);tMap.put(s4, 4);tMap.put(s2, 2);tMap.put(s3, 3);tMap.put(s1, 100);tMap.put(s3, null);tMap.remove(s3);Iterator<Entry<Student, Integer>> iterr = tMap.entrySet().iterator();while (iterr.hasNext()) {Entry<Student, Integer> e = iterr.next();if (e.getKey().equals(s4)) {iterr.remove();}}/** Caused Exception java.util.ConcurrentModificationExceptionfor (Entry<Student, Integer> e : tMap.entrySet()) {if (e.getValue() == 2) {tMap.remove(e.getKey());}}*/Iterator<Entry<Student, Integer>> iter = tMap.entrySet().iterator();while (iter.hasNext()) {Entry<Student, Integer> e = iter.next();System.out.println(e.getKey().toString() + " " + e.getValue());}for (Entry<Student, Integer> e : tMap.entrySet()) {System.out.println(e.getKey().toString() + " " + e.getValue());}}}class Student implements Comparable<Student> {private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}/** * @return the name */public String getName() {return name;}/** * @param name the name to set */public void setName(String name) {this.name = name;}/** * @return the age */public int getAge() {return age;}/** * @param age the age to set */public void setAge(int age) {this.age = age;}/** * @return the height */public double getHeight() {return height;}/** * @param height the height to set */public void setHeight(double height) {this.height = height;}@Overridepublic int hashCode() {int result = 1;final int prime = 31;result = result*prime + name == null ? 0 : name.hashCode();result = result*prime + age;result = result*prime + (int) Double.doubleToLongBits(height);return result;}@Overridepublic boolean equals(Object obj) {if (this == obj) {return true;}if (null == obj) {return false;}if (!(obj instanceof Student)) {return false;}Student s = (Student) obj;if (name != null) {if (s.name == null || !name.equals(s.name)) {return false;}} else {if (s.name != null) {return false;}}if (age != s.age) {return false;}if (Double.doubleToLongBits(height) != Double.doubleToLongBits(s.height)) {return false;}return true;}@Overridepublic String toString() {String str = "";str = name + " " + age + " " + height;return str;}@Overridepublic int compareTo(Student o) {if (this.age < o.age) {return -1;} else if (this.age > o.age) {return 1;}return 0;}}class StudentCmp implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {if (o2.getAge() < o1.getAge()) {return -1;} else if (o2.getAge() > o1.getAge()) {return 1;}return 0;}}

最后某些特殊情况下,可能需要基于value排序,下面的代码实现了基于value排序,并考虑了value为null的情况

List<Entry<Student, Integer>> lEntry = new ArrayList<Entry<Student, Integer>>(tMap.entrySet());Collections.sort(lEntry, new Comparator<Entry<Student, Integer>> () {@Overridepublic int compare(Entry<Student, Integer> o1, Entry<Student, Integer> o2) {if (o1.getValue() != null && o2.getValue() != null) {return o2.getValue() - o1.getValue();} else if (o1.getValue() == null) {return 1;} else {return -1;}}});





0 0
原创粉丝点击