赞
踩
(1)构造函数中new Comparator,匿名内部类,重写compare 方法。
(2)实体类实现Comparable,重写compareTo方法。
实体类:
public class Person { private final String name; private final int handsome;//帅气值 public String getName() { return name; } public int getHandsome() { return handsome; } public Person(String name, int handsome) { super(); this.name = name; this.handsome = handsome; } public Person() { name=null; handsome=0; } @Override public String toString() { return "名字:" + name + ", 帅气值: " + handsome + "\n"; } }
自定义排序:
public class TreeMapDemo { public static void main(String[] args) { Person p1 = new Person("刘德华", 100); Person p2 = new Person("赵小花", 30); Person p3 = new Person("王大华", 90); Person p4 = new Person("邓小二", 600); Person p5 = new Person("杨柳", 50); // 使用排序的业务类(匿名内部类) TreeMap<Person, String> map = new TreeMap<Person, String>( new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o1.getHandsome() - o2.getHandsome();//帅气值升序 } }); map.put(p1, "ll"); map.put(p2, "ll"); map.put(p3, "ll"); map.put(p4, "ll"); map.put(p5, "ll"); for(Person per:map.keySet()){ System.out.println(per.getName()+"的帅气值: "+per.getHandsome()); } // // 查看键 // Set<Person> persons = map.keySet(); // // System.out.println(persons); } }
运行结果:
实体类:实现comparable接口,重写compareto方法。
public class Worker implements Comparable<Worker>{ private String type;//工种 private double salary; public String getType() { return type; } public void setType(String type) { this.type = type; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Worker(String type, double salary) { super(); this.type = type; this.salary = salary; } @Override public String toString() { return "工种:" + type + ",工资:" + salary + "\n"; } public Worker() { } public int compareTo(Worker o){//工资升序 return (this.getSalary()-o.getSalary())>0?1:(this.getSalary()-o.getSalary())==0?0:-1; } }
应用:
public class TreeMapDemo02 { public static void main(String[] args) { Worker w1 = new Worker("回收员", 12000); Worker w2 = new Worker("思想者", 60000); Worker w3 = new Worker("农民工", 30000); Worker w4 = new Worker("打字员", 5000); Worker w5 = new Worker("售货员", 10000); // 使用排序的实体类实现 TreeMap<Worker, String> map = new TreeMap<>(); map.put(w1, "ll"); map.put(w2, "ll"); map.put(w3, "ll"); map.put(w4, "ll"); map.put(w5, "ll"); for(Worker per:map.keySet()){ System.out.println(per.getType()+"的工资: "+per.getSalary()); } } }
运行结果:
与TreeMap完全相同。
(1)构造函数中new Comparator,匿名内部类,重写compare 方法。
(2)实体类实现Comparable,重写compareTo方法。
public class TreeSetDemo { public static void main(String[] args) { Person p1 = new Person("刘德华", 100); Person p2 = new Person("赵小花", 30); Person p3 = new Person("王大华", 90); Person p4 = new Person("邓小二", 600); Person p5 = new Person("杨柳", 50); // 使用排序的业务类(匿名内部类) TreeSet<Person> persons = new TreeSet<Person>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return -(o1.getHandsome() - o2.getHandsome());//降序 } }); //treeset在添加数据的时候,就进行排序了。 //数据更改不会影响原来的顺序。 //treeset在使用时,不要修改数据,否则可能会造成数据重复。 //可以用final修饰原对象 persons.add(p1); persons.add(p2); persons.add(p3); persons.add(p4); persons.add(p5); for(Person per:persons){ System.out.println(per.getName()+" 帅气分数: "+per.getHandsome()); } } }
public class TreeSetDemo02 { public static void main(String[] args) { Worker w1 = new Worker("回收员", 12000); Worker w2 = new Worker("思想者", 60000); Worker w3 = new Worker("农民工", 30000); Worker w4 = new Worker("打字员", 5000); Worker w5 = new Worker("售货员", 10000); // 使用排序的实体类实现 TreeSet<Worker> workers = new TreeSet<>(); workers.add(w1); workers.add(w2); workers.add(w3); workers.add(w4); workers.add(w5); System.out.println(workers); }
TreeSet在添加数据的时候,就进行排序了。所以在之后操作更改数据,不会影响原来的排序。
所以在使用TreeSet的时候,需要注意不要修改数据,否则会造成数据错误。
当然,我们可以用final修饰原对象。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。