[置顶] java 集合框架 Set 集合之 HashSet TreeSet

set:

特点:无序的,长度可变的,不可重复的。

 

 

HashSet 的实现

对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单。

底层数据结构是 hash 表。

 

HashSet 保证元素的唯一性是通过元素的两个方法,hashCode 和 equals 来完成。

    -----如果元素的 HashCode 值相同,才会判断 equals 是否为true。

    -----如果元素的 HashCode 值不同,不会调用equals。

    -----注意:对于判断元素是否存在,以及删除等操作,依赖的方法是 hashcode 和 equals 方法。

 

 

class Name
{
    private String first; 
    private String last; 
    
    public Name(String first, String last) 
    { 
        this.first = first; 
        this.last = last; 
    } 

    public boolean equals(Object o) 
    { 
        if (this == o) 
        { 
            return true; 
        } 
        
	if (o.getClass() == Name.class) 
        { 
            Name n = (Name)o; 
            return n.first.equals(first) 
                && n.last.equals(last); 
        } 
        return false; 
    } 
}

public class HashSetTest
{
    public static void main(String[] args)
    { 
        Set<Name> s = new HashSet<Name>();
        s.add(new Name("abc", "123"));
        System.out.println(
            s.contains(new Name("abc", "123")));
    }
}


 

 

TreeSet 是依靠 TreeMap 来实现的。
TreeSet 是一个有序集合,TreeSet 中的元素将按照升序排列,缺省是按照自然排序进行排列,意味着 TreeSet 中的元素要实现 Comparable 接口。或者有一个自定义的比较器。
我们可以在构造TreeSet对象时,传递实现 Comparator 接口的比较器对象。

 

/**
 *
 *TreeSet:可以对 Set 集合中的元素进行排序。
 *   底层数据结构二叉树
 *   保证元素的唯一性的依据。
 *   compareTo 方法 return 0
 *   
 *   TreeSet 排序的第一种方式:让元素自身具备比较性
 *   元素需要实现 Compareable 接口,覆写 compareTo 方法。
 *   
 *
 *   TreeSet 的第二种排序方式:
 *   当元素自身不具备比较性时,或者具备的比较性不是所需要的。
 *   这时就需要让集合自身具备比较性。
 *
 */

 

TreeSet 排序的第一种方式:

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TreeSet ts = new TreeSet();
		ts.add(new Student("v1", 10));
		ts.add(new Student("v2", 11));
		ts.add(new Student("v3", 12));
		ts.add(new Student("v4", 13));
		ts.add(new Student("v1", 11));
		
		Iterator ite = ts.iterator();
		
		while(ite.hasNext()){
			Student st = (Student)ite.next();
			System.out.println(st.getName()+"<-->"+st.getAge());
		}
	}
	
}

class Student implements Comparable{ //实现 Comparable 接口,强制让元素具备比较性
	private String name;
	private int age;
	
	public Student(String name,int age) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.age = age;
	}
	
		
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		if(!(o instanceof Student))
			throw new RuntimeException("不是学生类对象!");
		Student stu = (Student)o;
		System.out.println(stu.age+"=========compareTo()====>"+stu.name);
		//主要因素
		if(this.age > stu.age){
			return 1;
		}else if(this.age == stu.age){
			//次要因素
			return this.name.compareTo(stu.name);
		}
		return -1;
	}
	
	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;
	}

}


TreeSet 的第二种排序方式:TreeSet 的第二种排序方式:

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TreeSet ts = new TreeSet(new myCompa());
		ts.add(new Student("v1", 10));
		ts.add(new Student("v2", 11));
		ts.add(new Student("v3", 12));
		ts.add(new Student("v4", 13));
		ts.add(new Student("v1", 11));
		
		Iterator ite = ts.iterator();
		
		while(ite.hasNext()){
			Student st = (Student)ite.next();
			System.out.println(st.getName()+"<-->"+st.getAge());
		}
	}
	
}


class myCompa implements Comparator{
	@Override
	public int compare(Object o1, Object o2) {
		Student st1 = (Student)o1;
		Student st2 = (Student)o2;
		
		int num = st1.getName().compareTo(st2.getName());
		if(num == 0){
			return new Integer(st1.getAge()).compareTo(new Integer(st2.getAge()));
		}
		return num;
	}
	
}


class Student{
	private String name;
	private int age;
	
	public Student(String name,int age) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.age = age;
	}
	
	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;
	}

}


 

 

 

 

 

 

你可能感兴趣的:(集合框架,set,hashset,TreeSet)