练习Java文档TreeSet

import java.util.*;

public class TreeSetTest
{
	public static void main(String[] args)
	{
		//书上的例子1
		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));

		//自然排序(就是java基本库中已经帮你实现好的compareTo方法)
		//例子2,为了说明被添加到TreeSet中的类一定要实现Comparable接口,否则会报错
		//var ts = new TreeSet();
		//ts.add(new Err());//是运行时错误

		//例子3,存入TreeSet的实例应该是同一个类的实例,因为进行比较的时候,要变成同一个类,否则的话,会报错
		//var ts = new TreeSet();
		//ts.add(new String("疯狂Java讲义"));
		//ts.add(new Date());//也是运行时错误

		//例子4,为了说明TreeSet判断元素相等的标准是compareTo返回0
		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);

		//例子5,为了说明修改了TreeSet中元素的重要变量所带来的后果
		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();
		//对第一个元素的count赋值
		first.count = 20;
		//取出最后一个元素
		var last = (R) ts.last();
		//对最后一个元素的count赋值,与第二个元素的count相同
		last.count = -2;
		//再次输出,将看到TreeSet里的元素处于无序状态,且有重复元素
		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);

		//定制排序,例子6
		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));

		//ts4.clear();
		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());
		//ts5.clear();
		//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;
	}
}

//例子4
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;
	}
}
//例子6
class M
{
	int age;
	public M(int age)
	{
		this.age =age;
	}
	public String toString()
	{
		return "M[age:"+age+"]";
	}
}

你可能感兴趣的:(笔记,java)