Day13---TreeSet的使用

TreeSet:

1.向TreeSet中添加元素必须是同一类的

public static void main(String str[])
    {
        TreeSet set = new TreeSet();
        set.add(10);
        set.add("10");
        for(Object o:set)
        {
            System.out.println(o);
        }
        
    }

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at java.lang.String.compareTo(String.java:111)
    at java.util.TreeMap.put(TreeMap.java:568)
    at java.util.TreeSet.add(TreeSet.java:255)
    at org.tizen.app.GenericDemo.main(GenericDemo.java:11)



    at java.util.TreeSet.add(TreeSet.java:255)

   public boolean add(E e) {
        return m.put(e, PRESENT)==null;
    }



    at java.util.TreeMap.put(TreeMap.java:568)

...

                cmp = k.compareTo(t.key);

...

2.可以按照进集合的指定顺序遍历set。如String,包装类按照从小到大的顺序i遍历

3.向TreeSet中添加自定义类,有两种排序:自然排序和定制排序

TreeSet按照指定顺序进行遍历,那是谁告诉它让它按照什么顺序?

答案是ThreeSet要实现一个具有排序功能的接口方法。


由于Set的无序性

Set中的元素又是按照hashcode方法来决定如何将set的对象放置于何处,这就是所谓的无序性

通过equals方法决定是否将对象添加到set中

所以要实现hashcode       equals      和     具有排序功能的接口方法     如 :Compartor的compare方法 Comparable的compareTo方法

package org.tizen.test;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import org.junit.Test;

public class TestMain {
	
	@Test
	public void test1()
	{
		Set s = new HashSet();
		s.add(10);
		s.add("AA");
		s.add(new person("lili",28));
		s.add(new person("lili",28));
		System.out.println(s.size());
		System.out.println(s);
	}
	
	@Test
	public void testComparator()
	{
		Comparator c = new student();
		Set s = new TreeSet(c);
		s.add(new student(1,"AA"));
		s.add(new student(1,"BB"));
		s.add(new student(12,"BB"));	
		s.add(new student(12,"BB"));		
		

		for(Object o:s)
		{
			System.out.println(o);
		}

		
	}
	
	@Test
	public void TestComparable()
	{
		Set s = new TreeSet();
		s.add(new person("AA",12));
		s.add(new person("BB",13));
		s.add(new person("BB",13));	
		s.add(new person("BB",14));
		
		for(Object o:s)
		{
			System.out.println(o);
		}
	}
	//如果不能直接操作类,就需要用这种方法
	@Test
	public void TestComparatorAgain()
	{
		Set s = new TreeSet(new Comparator()
		{
			@Override
			public int compare(Object o1, Object o2) {
				if(o1 instanceof student && o2 instanceof student)
				{
					student p1 = (student)o1;
					student p2 = (student)o2;
					int i = p1.getSchool().compareTo(p2.getSchool());
					if(i==0)
					{
						return p1.getID() - p2.getID();
					}
					else
					{
						return i;
					}
				}	
				return 0;
			}
	
		});
		s.add(new student(1,"AA"));
		s.add(new student(1,"BB"));
		s.add(new student(12,"BB"));	
		s.add(new student(12,"BB"));		
		

		for(Object o:s)
		{
			System.out.println(o);
		}
	
	}

}
class person implements Comparable
{
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		person other = (person) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	public person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "person [name=" + name + ", age=" + age + "]";
	}
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	private int age;
	@Override
	public int compareTo(Object o) {
		if(o instanceof person)
		{
			person p = (person)o;
			int i = this.name.compareTo(p.name);
			if(i==0)
			{
				return this.age - p.age;
			}
			else
				return i;
		}

		return 0;
	}
	
}

class student implements Comparator
{
	public student() {
		super();
	}
	@Override
	public String toString() {
		return "student [ID=" + ID + ", school=" + school + "]";
	}
	public student(int iD, String school) {
		super();
		ID = iD;
		this.school = school;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ID;
		result = prime * result + ((school == null) ? 0 : school.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		student other = (student) obj;
		if (ID != other.ID)
			return false;
		if (school == null) {
			if (other.school != null)
				return false;
		} else if (!school.equals(other.school))
			return false;
		return true;
	}
	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}
	public String getSchool() {
		return school;
	}
	public void setSchool(String school) {
		this.school = school;
	}
	private int ID;
	private String school;
	@Override
	public int compare(Object o1, Object o2) {
		if(o1 instanceof student && o2 instanceof student)
		{
			student p1 = (student)o1;
			student p2 = (student)o2;
			int i = p1.school.compareTo(p2.school);
			if(i==0)
			{
				return p1.ID - p2.ID;
			}
			else
			{
				return i;
			}
		}	
		return 0;
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值