/** * comparable 接口在类设计中就应该由类去实现,并实现compareTo方法。 接口位于java.lang包下。 对比方法叫compareTo comparator 接口在类已经完成之后还想再要比较。 接口位于java.util包下 对比方法叫compare */ TreeSet<TestChild> treeSet = new TreeSet<>(); treeSet.add(new TestChild("java02",2)); treeSet.add(new TestChild("java03",3)); treeSet.add(new TestChild("java01",1)); treeSet.add(new TestChild("java01",2)); /** * 输出 java01........1 java01........2 java02........2 java03........3 */ Iterator<TestChild> iterator = treeSet.iterator(); while(iterator.hasNext()){ TestChild testChild = iterator.next(); System.out.println(testChild.getName() +"........" + testChild.getAge()); } System.out.println("===================我是分割线=================="); //treeSet如果有比较器则优先使用比较器排序,否则用实现的comparable接口中的compareTo方法比较。 TreeSet<TestChild> treeSet1 = new TreeSet<>(new TestChildComparator()); treeSet1.add(new TestChild("java02",2)); treeSet1.add(new TestChild("java03",3)); treeSet1.add(new TestChild("java01",1)); treeSet1.add(new TestChild("java01",2)); iterator = treeSet1.iterator(); /** treeSet1.add(new TestChild("java01",2)); 失败的原因是比较器年龄相同返回0,hashSet看成相同元素,不添加。 输出 java03........3 java02........2 java01........1 */ while(iterator.hasNext()){ TestChild testChild = iterator.next(); System.out.println(testChild.getName() +"........" + testChild.getAge()); } System.out.println("===================我是分割线之二=================="); TreeSet<TestChild> treeSet2 = new TreeSet<>(); treeSet2.add(new TestChild("java02",2)); treeSet2.add(new TestChild("java03",3)); treeSet2.add(new TestChild("java01",1)); treeSet2.add(new TestChild("java01",2)); TestChild[] aaa = new TestChild[treeSet2.size()]; Arrays.sort(treeSet2.toArray(aaa),new TestChildComparator()); iterator = treeSet2.iterator(); /** 添加成功 treeSet2.add(new TestChild("java01",2)); 因为一开始么有用比较器,所以添加成功。 输出 java01........1 java01........2 java02........2 java03........3 */ while(iterator.hasNext()){ TestChild testChild = iterator.next(); System.out.println(testChild.getName() +"........" + testChild.getAge()); } System.out.println("===================我是分割线之三=================="); /** 输出 java03........3 java01........2 java02........2 java01........1 */ for (int i = 0 ; i < aaa.length ; i ++){ System.out.println(aaa[i].getName() +"........" + aaa[i].getAge());
}
private static class TestChildComparator implements Comparator<TestChild>{ @Override public int compare(TestChild o1, TestChild o2) { if(o1.getAge() > o2.getAge()){ return -1; }else if(o1.getAge() == o2.getAge()){ return 0; } return 1; } } private static class TestChild implements Comparable{ private String name; private int age; public TestChild(String name, int age) { 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 compareTo(Object o) { TestChild testChild = (TestChild)o; if(age > testChild.getAge()){ return 1; }else if(age == testChild.getAge()){ return name.compareTo(testChild.getName()); } return -1; } @Override public boolean equals(Object obj) { TestChild testChild = (TestChild)obj; return testChild.getAge() == testChild.getAge(); } }