EchoDemo's Blogs

Java中的Set

1、Set容器的特点

Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序。最常用的两个Set接口的实现类是HashSet和TreeSet。

2、HashSet及常用API

(1)HashSet扩展AbstractSet并实现Set接口,HashSet使用散列表(又称之为哈希表)进行存储。(HashSet底层实际上维护了一个HashMap,只是我们操作的是HashMap的key,而HashMap底层维护了哈希表)

(2)构造方法:HashSet();HashSet(Collection c);HashSet(int capacity);HashSet(int capacity,float fillRatio)。

(3)HashSet没有定义任何超过它的父类和接口提供的其他方法,散列集合没有确保其元素的顺序,因为散列处理通常不参与排序。

(4)举例

package com.iotech.set;

import java.util.HashSet;

public class HashSetDemo1 {

    public static void main(String[] args) {
        /*HashSet<String> hashSet=new HashSet<String>();
        hashSet.add("zhangsan");
        hashSet.add("lisi");
        hashSet.add("jay");
        hashSet.add("jack");
        System.out.println(hashSet.add("jay"));
        System.out.println(hashSet);*/

        HashSet<Student> hashSet=new HashSet<Student>(); 
        System.out.println(hashSet.add(new Student("张三", 20)));
        System.out.println(hashSet.add(new Student("李四", 30)));
        System.out.println(hashSet.add(new Student("张三", 20)));
        System.out.println(hashSet.size());
    }
}

class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        super();
        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;
    }
    @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;
        Student other = (Student) 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;
    }
}

3、TreeSet及常用API

(1)TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和检索很快。在存储了大量的需要进行快速检索的排序信息的情况下,TreeSet是一个很好的选择。(事实上,TreeSet的底层就是TreeMap实现的,只是我们操作的是TreeMap的Key)

(2)构造方法:TreeSet();TreeSet(Collection c);TreeSet(Comparator comp);TreeSet(SortedSet ss)。

(3)举例

package com.iotech.set;

import java.util.Iterator;
import java.util.TreeSet;


public class TreeSetDemo1 {

    public static void main(String[] args) {
        TreeSet<Person> treeSet=new TreeSet<Person>();
        treeSet.add(new Person("chenhao", 30));
        treeSet.add(new Person("lisi", 20));
        treeSet.add(new Person("wangwu", 10));
        treeSet.add(new Person("rose", 40));
        Iterator<Person> iterator=treeSet.iterator();
        while(iterator.hasNext()){
            Person person=iterator.next();
            System.out.println(person.getName()+"-->"+person.getAge());
        }
    }
}

class Person implements Comparable<Person>{
    private String name;
    private int age;

    public Person(String name, int age) {
        super();
        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;
    }
    @Override
    public int compareTo(Person o) {
        int x=this.age-o.age;
        if(x>0) return 1;
        else if(x<0) return -1;
        return 0;
    }
}
🐶 您的支持将鼓励我继续创作 🐶
-------------本文结束感谢您的阅读-------------