42 lines
1.1 KiB
Java
42 lines
1.1 KiB
Java
package com.inmind;
|
|
|
|
import com.inmind.test07.Student;
|
|
|
|
import java.util.Comparator;
|
|
import java.util.Iterator;
|
|
import java.util.TreeMap;
|
|
import java.util.TreeSet;
|
|
|
|
public class TreeSetDemo1 {
|
|
public static void main(String[] args) {
|
|
TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
|
|
|
|
@Override
|
|
public int compare(Student o1, Student o2) {
|
|
return o2.getId() - o1.getId();
|
|
}
|
|
});
|
|
Student s1 = new Student();
|
|
s1.setId(1);
|
|
Student s2 = new Student();
|
|
s2.setId(3);
|
|
Student s3 = new Student();
|
|
s3.setId(5);
|
|
treeSet.add(s1);
|
|
treeSet.add(s2);
|
|
treeSet.add(s3);
|
|
System.out.println(treeSet);
|
|
|
|
TreeMap<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
|
|
@Override
|
|
public int compare(Student o1, Student o2) {
|
|
return o2.getId() - o1.getId();
|
|
}
|
|
});
|
|
treeMap.put(s1, "<UNK>");
|
|
treeMap.put(s2, "<UNK>");
|
|
treeMap.put(s3, "<UNK>");
|
|
|
|
System.out.println(treeMap);
|
|
}
|
|
} |