自然排序(Comparable):
package Java集合;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
TreeMapTest treeMapTest = new TreeMapTest();
System.out.println("---------------自然排序----------------\n规则:先按照name从小到大排序,再按照age从大到小排序");
treeMapTest.test();
}
public void test(){
Persons2 persons2 = new Persons2("Mike", 18);
Persons2 persons22 = new Persons2("Joney", 28);
Persons2 persons23 = new Persons2("Dan", 14);
Persons2 persons24 = new Persons2("Tiye", 16);
Persons2 persons25 = new Persons2("Tiye", 22);
TreeMap treeMap = new TreeMap();
treeMap.put(persons2,null);
treeMap.put(persons22,null);
treeMap.put(persons23,null);
treeMap.put(persons24,null);
treeMap.put(persons25,null);
Set entrySet = treeMap.entrySet();
Iterator iterator = entrySet.iterator();
for (Object object : entrySet) {
System.out.println(object);
}
}
}
class Persons2 implements Comparable{
String name;
int age;
public Persons2(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int compareTo(Object o) {
if(o instanceof Persons2) {
Persons2 p = (Persons2)o;
int re = this.name.compareTo(p.name);
if(re == 0) {
return -Integer.compare(this.age, ((Persons2) o).age);
}else {
return re;
}
}
throw new RuntimeException("输入的参数不符合要求");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
定制排序(Comparator):
package Java集合;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
TreeMapTest treeMapTest = new TreeMapTest();
System.out.println("\n---------------定制排序----------------\n规则:先按照age从大到小排序,再按照name从小到大排序");
treeMapTest.test2();
}
public void test2() {
Persons2 persons2 = new Persons2("Mike", 18);
Persons2 persons22 = new Persons2("Joney", 28);
Persons2 persons23 = new Persons2("Dan", 14);
Persons2 persons24 = new Persons2("Tiye", 16);
Persons2 persons25 = new Persons2("Tiye", 22);
Comparator comparator = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Persons2 && o1 instanceof Persons2) {
Persons2 p = (Persons2)o1;
Persons2 p2 = (Persons2)o2;
int re = -Integer.compare(p.age, ((Persons2) p2).age);
if(re == 0) {
return p.name.compareTo(p2.name);
}else {
return re;
}
}
throw new RuntimeException("输入的参数不符合要求");
}
};
TreeMap treeMap = new TreeMap(comparator);
treeMap.put(persons2,null);
treeMap.put(persons22,null);
treeMap.put(persons23,null);
treeMap.put(persons24,null);
treeMap.put(persons25,null);
Set entrySet = treeMap.entrySet();
Iterator iterator = entrySet.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
class Persons2 implements Comparable{
String name;
int age;
public Persons2(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int compareTo(Object o) {
if(o instanceof Persons2) {
Persons2 p = (Persons2)o;
int re = this.name.compareTo(p.name);
if(re == 0) {
return -Integer.compare(this.age, ((Persons2) o).age);
}else {
return re;
}
}
throw new RuntimeException("输入的参数不符合要求");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}