- package it.cast2;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.TreeSet;
- public class TreeSetDemo {
- public static void main(String[] args) {
-
- Comparator<TestStudet> myComparetor = new Comparator<TestStudet>() {
- @Override
- public int compare(TestStudet o1, TestStudet o2) {
-
- int result = o1.getAge() - o2.getAge();
- if (result == 0) {
- result = o1.getNameString().compareTo(o2.getNameString());
- }
- return result;
- }
- };
-
- //MyComparetor myComparetor = new MyComparetor();
- TreeSet<TestStudet> tSet = new TreeSet<>(myComparetor);
- TestStudet testStudet = new TestStudet("1zhangsan", 23);
- TestStudet testStudet1 = new TestStudet("1zhangsan2", 232);
- TestStudet testStudet2 = new TestStudet("1zhangsan1", 232);
-
- tSet.add(testStudet);
- tSet.add(testStudet1);
- tSet.add(testStudet2);
- Iterator<TestStudet> iterator = tSet.iterator();
- while (iterator.hasNext()) {
- TestStudet testStudet3 = iterator.next();
- System.out.println(testStudet3);
- }
- }
- }
复制代码
|
|