import java.util.*;
public class TreeSetTest
{
public static void main(String[] args)
{
TreeSet nums = new TreeSet();
nums.add(5);
nums.add(2);
nums.add(10);
nums.add(-9);
System.out.println(nums);
System.out.println(nums.first());
System.out.println(nums.last());
System.out.println(nums.headSet(6));
System.out.println(nums.tailSet(5));
System.out.println(nums.subSet(-3,4));
var set = new TreeSet();
var z1 = new Z(6);
set.add(z1);
System.out.println(set.add(z1));
System.out.println(set);
((Z)(set.first())).age = 9;
System.out.println(((Z)(set.last())).age);
var ts = new TreeSet();
ts.add(new R(5));
ts.add(new R(-3));
ts.add(new R(9));
ts.add(new R(-2));
System.out.println(ts);
var first = (R) ts.first();
first.count = 20;
var last = (R) ts.last();
last.count = -2;
System.out.println(ts);
System.out.println(ts.remove(new R(-2)));
System.out.println(ts);
System.out.println(ts.remove(new R(5)));
System.out.println(ts);
System.out.println();
System.out.println("定制排序");
ts = new TreeSet((o1,o2) ->
{
var m1 = (M) o1;
var m2 = (M) o2;
return m1.age > m2.age ? -1 : m1.age < m2.age ? 1:0;
});
ts.add(new M(5));
ts.add(new M(-3));
ts.add(new M(9));
System.out.println(ts);
TreeSet ts2 = new TreeSet();
ArrayList list = new ArrayList();
list.add("a");
ts2 = new TreeSet(list);
System.out.println(ts2);
ts2 = new TreeSet((o1 , o2 ) ->
{
return (int)o1 > (int)o2 ? -1 : (int)o1 < (int)o2 ? 1 : 0;
});
ts2.add(1);
ts2.add(2);
System.out.println(ts2);
TreeSet ts3 = new TreeSet();
ts3.add(7);
ts3.add(8);
TreeSet ts4 = new TreeSet(ts3);
System.out.println(ts4);
ts4.add(111);
ts4.addAll(ts2);
System.out.println(ts4);
System.out.println(ts4.ceiling(-1));
TreeSet ts5 = (TreeSet) ts4.clone();
System.out.println(ts5);
System.out.println(ts5.comparator());
System.out.println(ts2.comparator());
System.out.println(ts5.contains(111));
System.out.println(ts5.contains(6));
Iterator it = ts5.descendingIterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println(ts5);
NavigableSet ns = ts5.descendingSet();
System.out.println(ns);
System.out.println(ts5.first());
System.out.println(ts5.floor(1111));
SortedSet ss = ts5.headSet(8);
System.out.println(ss);
NavigableSet ns2 = ts5.headSet(8,false);
System.out.println(ns2);
System.out.println(ts5.higher(100));
System.out.println(ts5.isEmpty());
it = ts5.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println(ts5.last());
System.out.println(ts5.lower(-7));
System.out.println(ts5);
System.out.println(ts5.pollFirst());
System.out.println(ts5);
System.out.println(ts5.pollLast());
System.out.println(ts5);
System.out.println(ts5.remove(-7));
System.out.println(ts5);
System.out.println(ts5.size());
Spliterator spli = ts5.spliterator();
System.out.println(spli.characteristics());
ts5.add(9);
ts5.add(10);
ts5.add(11);
ts5.add(12);
ts5.add(13);
ts5.add(14);
ts5.add(15);
System.out.println(ts5);
System.out.println(ts5.subSet(10,true,13,false));
System.out.println(ts5.subSet(10,13));
System.out.println(ts5.tailSet(13));
System.out.println(ts5.tailSet(13,false));
}
}
class Err{}
class Z implements Comparable
{
int age ;
public Z(int age)
{
this.age = age;
}
public boolean equals(Object obj)
{
return true;
}
public int compareTo(Object obj)
{
return 1;
}
}
class R implements Comparable
{
int count;
public R(int count)
{
this.count = count;
}
public String toString()
{
return "R[count:"+count+"]";
}
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(obj != null && obj.getClass() == R.class)
{
var r = (R) obj;
return this.count == r.count;
}
return false;
}
public int compareTo(Object obj)
{
var r = (R) obj;
return count>r.count ? 1:
count < r.count ? -1: 0;
}
}
class M
{
int age;
public M(int age)
{
this.age =age;
}
public String toString()
{
return "M[age:"+age+"]";
}
}
|