HashSet And TreeSet

Set接口

Set不允许包含相同的元素,如果要将两个相同的元素加入同一个集合中,add方法会返回false。Set判断两个对象相同不是使用==运算符,而是根据equals方法。也就是说,只要两个对象用equals方法比较返回true,Set就不 会接受这两个对象,并且最多包含一个 null 元素。

HashSet与TreeSet都是基于Set接口的实现类。其中TreeSet是Set的子接口SortedSet的实现类。Set接口及其子接口、实现类的结构如下所示:

     |——SortedSet接口——TreeSet实现类

Set接口——|——HashSet实现类
|——LinkedHashSet实现类

HashSet

  1. 不能保证元素的排列顺序,顺序有可能发生变化
  2. 不是同步的
  3. 集合元素可以是null,但只能放入一个null
  4. 当向HashSet结合中存入一个元素时,HashSet会调用该对象的hashCode()方法来得到该对象的hashCode值,简单而言,HashSet集合判断两个元素相等的标准是两个对象通过equals方法比较相等,并且两个对象的hashCode()返回值相等。
    注意,如果要把一个对象放入HashSet中,重写该对象对应类的equals方法,也应该重写其hashCode()方法。其规则是如果两个对 象通过equals方法比较返回true时,其hashCode也应该相同。另外,对象中用作equals比较标准的属性,都应该用来计算 hashCode的值。
public class HashsetTest {
    public static void main(String[] args) {
        HashSet hs = new HashSet();
        hs.add("one");
        hs.add("two");
        hs.add("three");
        hs.add("four");
        hs.add(new Student(1, "zhangsan"));
        hs.add(new Student(2,"lisi"));
        hs.add(new Student(3,"wangwu"));
        hs.add(new Student(4,"zhaosan"));

        Iterator it=hs.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}
    class  Student{ //HashSet要重写hashCose和equals方法
    int num;
    String name;
    Student(int num,String name){
        this.num=num;
        this.name=name;
    }

        @Override
        public String toString() {
            return "num :"+num+" name:"+name;
        }

        @Override
        public int hashCode() {
            return num*name.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            Student s= (Student) obj;
        return num==s.num&&s.name.equals(s.name);
        }
    }

TreeSet类
TreeSet是SortedSet接口的唯一实现类,TreeSet可以确保集合元素处于排序状态。TreeSet支持两种排序方式,自然排序 和定制排序,其中自然排序为默认的排序方式。向TreeSet中加入的应该是同一个类的对象。

TreeSet判断两个对象不相等的方式是两个对象通过equals方法返回false,或者通过CompareTo方法比较没有返回0

  1. 自然排序
    自然排序使用要排序的元素的CompareTo(Object o)方法来比较元素大小关系,然后按照升序进行排列。
    2.定制排序
    定制排序使用Comparator接口,实现int compare(To1,T o2)方法。
public class TreeSetTest {
	public static void main(String[] args) {

		TreeSet<Students> ts = new TreeSet<Students>(new CompareToStudent());
		ts.add(new Students(2, "zhangshan"));
		ts.add(new Students(3, "lishi"));
		ts.add(new Students(1, "wangwu"));
		ts.add(new Students(4, "maliu"));

		Iterator<Students> it = ts.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}

class Students implements Comparable<Students> {
	int num;
	String name;

	Students(int num, String name) {
		this.num = num;
		this.name = name;
	}

	public String toString() {
		return num + ":" + name;
	}

	@Override
	public int compareTo(Students o) {
		int result;
		Students s = (Students) o;
		result = num > s.num ? 1 : (num == s.num ? 0 : -1);
		if (result == 0) {
			result = name.compareTo(s.name);
		}
		return result;
	}
}

class CompareToStudent implements Comparator<Object> {
	public int compare(Object o1, Object o2) {
		Students s1 = (Students) o1;
		Students s2 = (Students) o2;
		int rulst = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
		if (rulst == 0) {
			rulst = s1.name.compareTo(s2.name);
		}
		return rulst;
	}
}

与HashSet相比,TreeSet还提供了几个额外的方法:
1、Comparator comparator():返回当前set使用的Comparator,或者返回null,表示以自然方式排序。
2、Object first():返回集合中的第一个元素。
3、Object last():返回集合中的最后一个元素。
4、Object lower(Object e):返回集合中位于指定元素之前的一个元素。
5、Object higher(Object e):返回集合中位于指定元素之后的一个元素。
6、SortedSet subSet(from Element,to Element):返回此set的子集合,范围从from Element到to Element(闭包)。
7、SortedSet headSet(toElement):返回此Set的子集,由小于toElement的元素组成。
8、SortedSet tailSet(fromElement):返回此Set的子集,由大于等于fromElement的元素组成。
LinkedHashSet
素hashCode值来决定元素的存储位置,但它同时用链表来维护元素的次序,这样使得元素看起来是以插入的顺序保存的,也就是说,当遍历LinkedHashSet集合元素时,它将会按元素的添加顺序来访问集合里的元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值