Java之Set接口

Set接口继承自Collection接口。Set接口时无序的,并且以某种规则保证存入的元素不出现重复。

Set接口主要由两个实现类 HashSet TreeSet。

HashSet是根据对象的哈希值来确定元素在集合中的存储位置,因此由良好的存储和查找的性能

TreeSet以二叉树做为存储结构,可以对元素进行排序。

 

HashSet集合:

定义:HashSet是Set接口的一个实现类,它所存储的元素是不能重复的,并且元素是无序的。

原理:当像HashSet存入一个元素时,首先会调用该对象的HashCode()确认元素的存储位置,然后在调用对象的equals()来确保改为值没有重复的元素。所以一般来说我们都会重写这两个方法,以确保集合中没有重复元素。

用法:

创建HashSet集合

import java.util.HashSet;
import java.util.Iterator;

public class HashSetExample {
    public static void main(String args[]){
        HashSet<String> hashSet = new HashSet<>();//创建集合HashSet
        hashSet.add("intel"); //添加元素
        hashSet.add("amd");
        hashSet.add("nvidia");
        hashSet.add("ati");
        hashSet.add("ati");
        //使用Iterator 迭代器 遍历集合
        Iterator<String> iterator = hashSet.iterator();
        while(iterator.hasNext()){
            String element = iterator.next();
            System.out.println(element);
        }
    }
}
//运行结果
//ati
//amd
//intel
//nvidia

add()方法添加元素

使用迭代器Iterator对象遍历集合。

代码中由两语句 hashSet.add("ati");    hashSet.add("ati");但是结果只有一个,所以HashSet是无重复的集合。

在调用add()方法时,首先调用当前对象的hashCode()方法,获取对象的哈希值,然后根据哈希值算出存储位置,若该位置已有元素,则将调用该对象equals()方法,让当前存入元素一次和该位置上的元素进行比较,返回false就存入该元素,否则丢弃。

如果想改变注意现象,需要重写hashCode()和equals()两个方法。

public class HashSetExample {
    class Student {
        private String id;
        private String name;

        public Student(){}
        public Student(String id,String name){
            this.id = id;
            this.name = name;
        }
        /*public int hashCode(){
            return id.hashCode();
        }
        public boolean equals(Object obj){
            if(this == obj)
                return true;

            if(! (obj instanceof Student))
                return  false;

            Student stu= (Student) obj;
            return  this.id.equals(stu.id);
        }*/
        public String toString(){
            return this.id+" --- "+this.name;
        }
    }
    public static void main(String args[]){
        HashSet<Student> hashSet = new HashSet<>();//创建集合HashSet
        HashSetExample hashSetExample = new HashSetExample();
        HashSetExample.Student stu1 = hashSetExample.new Student("1","jc");
        HashSetExample.Student stu2 = hashSetExample.new Student("12","jd");
        HashSetExample.Student stu3 = hashSetExample.new Student("1","jc");
        HashSetExample.Student stu4 = hashSetExample.new Student("1","ac");
        hashSet.add(stu1); //添加元素
        hashSet.add(stu2); //添加元素
        hashSet.add(stu3); //添加元素
        hashSet.add(stu4); //添加元素
        //使用Iterator 迭代器 遍历集合
        Iterator<Student> iterator = hashSet.iterator();
        while(iterator.hasNext()){
            HashSetExample.Student student = iterator.next();
            System.out.println(student);
        }
    }
}

//重写方法前结果
1 --- jc
12 --- jd
1 --- jc
1 --- ac
import java.util.HashSet;
import java.util.Iterator;

public class HashSetExample {
    class Student {
        private String id;
        private String name;

        public Student(){}
        public Student(String id,String name){
            this.id = id;
            this.name = name;
        }
        public int hashCode(){
            return id.hashCode();
        }
        public boolean equals(Object obj){
            if(this == obj)
                return true;

            if(! (obj instanceof Student))
                return  false;

            Student stu= (Student) obj;
            return  this.id.equals(stu.id);
        }
        public String toString(){
            return this.id+" --- "+this.name;
        }
    }
    public static void main(String args[]){
        HashSet<Student> hashSet = new HashSet<>();//创建集合HashSet
        HashSetExample hashSetExample = new HashSetExample();
        HashSetExample.Student stu1 = hashSetExample.new Student("1","jc");
        HashSetExample.Student stu2 = hashSetExample.new Student("12","jd");
        HashSetExample.Student stu3 = hashSetExample.new Student("1","jc");
        HashSetExample.Student stu4 = hashSetExample.new Student("11","jc");
        hashSet.add(stu1); //添加元素
        hashSet.add(stu2); //添加元素
        hashSet.add(stu3); //添加元素
        hashSet.add(stu4); //添加元素
        //使用Iterator 迭代器 遍历集合
        Iterator<Student> iterator = hashSet.iterator();
        while(iterator.hasNext()){
            HashSetExample.Student student = iterator.next();
            System.out.println(student);
        }
    }
}

//重写方法后 运行结果
11 --- jc
1 --- jc
12 --- jd

TreeSet集合:以后更新

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值