一点小心得
我今天心里其实已经有决断了,后续快速开始执行。
程序员代码境界
-
what:所有知识,都要知道是什么。
输入是什么?
输出是什么?
有何使用禁忌?
-
why:20%常用知识,需要了解为什么。
为何需要输入这些?
为何输出是这些?(是什么逻辑实现了这样的输出)
使用禁忌来源于哪里?
-
how:20%常用知识,需要了解怎么用。
需要实现什么功能时,要使用这个技术?
跨界
产品
营销
投资
第127集:引用类型_排序容器_TreeSet与TreeMap
ArrayList容器本身是不可以排序的,需要借助工具类Collections.sort方法。
TreeSet是Set接口的实现类,两个特点:数据元素可以排序且不可重复
TreeSet和HashSet的区别:HashSet必须重写hashcode和equals方法,而TreeSet不用,它只要求它装的元素实现排序。(去重:比较等于0即重复)。
-
以下两种排序法二选一:
元素可排序:Comparable+c omaparaTo;
package test127; public class Person { private final String name; private final int handsome; // public void setName(String name){ // this.name = name; // } public String getName(){ return name; } // public void setHandSome(int handsome){ // this.handsome = handsome; // } public int getHandSome(){ return handsome; } public Person(){ name = null; handsome = 0; } public Person(String name,int handsome){ this.name = name; this.handsome = handsome; } public String toString(){ return "名称:"+this.name+" 颜值:"+this.handsome+"\n"; } }
package test127; import java.util.Comparator; import java.util.TreeSet; public class TestTreeSet { public static void main(String[] args){ Person a = new Person("zhang",1000); Person b = new Person("zhang1",2000); Person c = new Person("zhang2",3000); Person d = new Person("zhang3",4000); Person e = new Person("zhang4",5000); Person f = new Person("zhang5",6000); TreeSet<Person> persons = new TreeSet<Person>( new Comparator<Person>(){ public int compare(Person o1,Person o2){ return -(o1.getHandSome()-o2.getHandSome()); } } ); persons.add(a); // TreeSet在添加数据时就完成了数据排序 persons.add(b); persons.add(c); persons.add(d); persons.add(e); persons.add(f); System.out.println(persons); //d.setHandSome(-1000);//这里一改值就会出问题,扰乱排序 //e.setHandSome(6000);//也可能导致重复 //杜绝的方法就是用final System.out.println(persons); } }
排序业务类:Comparator+compare
package test127; public class Worker implements Comparable<Worker> { private String type; private int salary; public String getType() { return type; } public void setType(String type) { this.type = type; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public Worker(String type, int salary) { super(); this.type = type; this.salary = salary; } public Worker() { } public int compareTo(Worker anotherWorker){ return this.getSalary() - anotherWorker.getSalary(); } public String toString(){ return "名称:"+this.getType()+" 工资:"+this.getSalary(); } }
package test127; import java.util.TreeSet; public class TestTreeSet2 { public static void main(String[] args){ Worker a = new Worker("zhang",1000); Worker b = new Worker("zhang1",2000); Worker c = new Worker("zhang2",3000); TreeSet<Worker> Workers = new TreeSet<Worker>(); Workers.add(a); // TreeSet在添加数据时就完成了数据排序 Workers.add(b); Workers.add(c); System.out.println(Workers); //d.setHandSome(-1000);//这里一改值就会出问题,扰乱排序 //e.setHandSome(6000);//也可能导致重复 //杜绝的方法就是用final System.out.println(Workers); } }
-
TreeMap
Comparable+comparaTo
package test127; import java.util.Comparator; import java.util.Set; import java.util.TreeMap; public class TestTreeMap { public static void main(String[] args){ Person a = new Person("zhang",1000); Person b = new Person("zhang1",2000); Person c = new Person("zhang2",3000); Person d = new Person("zhang3",4000); Person e = new Person("zhang4",5000); Person f = new Person("zhang5",6000); TreeMap<Person,String> persons = new TreeMap<Person,String>( new Comparator<Person>(){ public int compare(Person o1,Person o2){ return -(o1.getHandSome()-o2.getHandSome()); } } ); persons.put(a,"bj"); persons.put(b,"bj"); persons.put(c,"bj"); persons.put(d,"bj"); persons.put(e,"bj"); persons.put(f,"bj"); Set<Person> myPersons = persons.keySet(); System.out.println(persons); } }
Comparator+compare
package test127; import java.util.TreeMap; public class TestTreeMap2 { public static void main(String[] args){ Worker a = new Worker("zhang",1000); Worker b = new Worker("zhang1",2000); Worker c = new Worker("zhang2",3000); TreeMap<Worker,String> Workers = new TreeMap<Worker,String>(); Workers.put(a,"1"); // TreeSet在添加数据时就完成了数据排序 Workers.put(b,"1"); Workers.put(c,"1"); System.out.println(Workers.keySet()); } }