Set集合接口、HashSet集合、LinkedHashSet集合、可变参数

Set接口

java.util.Set接口 extends Collection

Set接口的特点:

1.不能出现重复的元素

2.没有索引,没有带索引的方法,也不能使用普通的for循环遍历

里面包含的方法和Collection里面的方法差不多

java.util.HashSet集合implements Set接口

HashSet特点:

1.不允许出现重复的元素

2.没有索引,没有带索引的方法,也不能使用普通的for循环遍历

3.是一个无序集合,存储元素和取出元素的顺序可能不一致

4.底层是一个哈希表结构(查询的速度非常快)

package mycollection;

import java.util.HashSet;
import java.util.Set;

public class TestHashSet01 {
    public static void main(String[] args) {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(3);
        set.add(2);
        set.add(1);

        for (Integer i : set) {
            System.out.print(i+"\t");//1   2  3
        }
    }
}
哈希值

哈希值:是一个十进制的整数,由系统随机给出(就是对象的地址值,是一个逻辑值,是一个模拟出来得到的地址,不是数据实际存储的物理地址)

在object类中有一个方法,可以获取对象的哈希值

public native int hashCode();

native:代表调用本地系统的操作方法

package mycollection;

public class TestHashCode01 {
    public static void main(String[] args) {
       Person p1 = new Person();
       System.out.println(p1.hashCode());//460141958

       Person p2 = new Person();
       System.out.println(p2.hashCode());//1163157884

       /*toString的源码:
       * return getClass().getName() + "@" + Integer.toHexString(hashCode());(后面将哈希码转化为十六进制)*/

        System.out.println(p1);//mycollection.Person@1b6d3586  后面数字1b6d3586是460141958的十六进制表达
        System.out.println(p2);//mycollection.Person@4554617c

        /*string类的哈希值
        *   string类重写了继承object类的hashCode()方法*/
        String str1 = new String("abc");
        String str2 = new String("abc");
        System.out.println(str1.hashCode());//96354
        System.out.println(str2.hashCode());//96354  字符串相同,两者哈希码相同

        //巧合,字符串不一样,哈希码一样
        System.out.println("重地".hashCode());//1179395
        System.out.println("通话".hashCode());//1179395

    }
}

class Person{
    //从object类中继承来的hashCode()方法可以重写
    /*@Override
    public int hashCode() {
        return 1;
    }*/
}
HashSet的存储数据的结构:哈希表结构

HashSet集合存储不重复的元素的原理:

前提:存储的元素必须重写hashCode方法和equals方法

HashSet存储自定义类型元素

set集合保存的元素唯一:

​ 存储的元素(string,integer,....Student,Person),必须重写hashCode方法和equals方法

当存储的元素没重写hashCode方法和equals方法时:

package mycollection;

import java.util.HashSet;

//HashSet存储自定义类型元素
//set集合不允许存储相同姓名和年龄的学生
public class TestHashSet02 {
    public static void main(String[] args) {
        HashSet<Student> set = new HashSet<>();
        Student s1 = new Student("小明",12);
        Student s2 = new Student("小化",13);
        Student s3 = new Student("小明",12);

        set.add(s1);
        set.add(s2);
        set.add(s3);
        System.out.println(set);

        System.out.println(s1.hashCode());//460141958
        System.out.println(s3.hashCode());//1956725890
        System.out.println(s1.equals(s3));//false
        System.out.println(set);//[Student{name='小明', age=12}, Student{name='小化', age=13}, Student{name='小明', age=12}]set中存储了相同的学生
        
    }
}

class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

当存储的元素重写hashCode方法和equals方法时:

package mycollection;

import java.util.HashSet;
import java.util.Objects;

//HashSet存储自定义类型元素
//set集合不允许存储相同姓名和年龄的学生
public class TestHashSet02 {
    public static void main(String[] args) {
        HashSet<Student> set = new HashSet<>();
        Student s1 = new Student("小明",12);
        Student s2 = new Student("小化",13);
        Student s3 = new Student("小明",12);

        set.add(s1);
        set.add(s2);
        set.add(s3);
        System.out.println(set);

        System.out.println(s1.hashCode());//23458766
        System.out.println(s3.hashCode());//23458766
        System.out.println(s1.equals(s3));//true
        System.out.println(set);//[Student{name='小化', age=13}, Student{name='小明', age=12}],没有重复的学生
    }
}

class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //重写了hashCode方法和equals方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, 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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
LinkedHashSet集合

java.util.LinkedHashSet集合 extends HashSet集合

LinkedHashSet集合特点:

底层是一个哈希表(数组+链表/红黑树)+链表:多了一条链表(记录元素的存储顺序),保证元素有序

package mycollection;

import java.util.HashSet;
import java.util.LinkedHashSet;

public class TestLinkedHashSet01 {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("abc");
set.add("abc");
set.add("哈哈哈哈");
set.add("哦哦哦");
System.out.println(set);//[哈哈哈哈, abc, 哦哦哦] 不允许重复 无序

    LinkedHashSet&lt;String&gt; linked = new LinkedHashSet&lt;&gt;();
    linked.add(&quot;abc&quot;);
    linked.add(&quot;abc&quot;);
    linked.add(&quot;哈哈哈哈&quot;);
    linked.add(&quot;哦哦哦&quot;);
    System.out.println(linked);//[abc, 哈哈哈哈, 哦哦哦],不允许重复  有序,按添加顺序
}

}

 

可变参数:是jdk1.5之后出现的新特性

使用前提:

参数列表的数据类型已经确定,但参数个数不确定,就可以使用可变参数

使用格式:方法定义时使用

修饰符 返回值类型 方法名(数据类型... 变量名){}

可变参数的原理:

可变参数的底层就是一个数组,根据传递参数的个数不同,会创建不同长度的数组,来存储这些参数

传递的参数个数,可以是0个(不传递),1,2...多个

注意事项:

1.一个方法的参数列表,只能有一个可变参数

2.如果方法的参数有多个,那可变参数必须在参数列表末尾

public int add(int... arr){
    int sum = 0;
    for (int i : arr) {
        sum += i;
    }
    return sum;
}
public void add2(double d,float f,int...arr){
}

**可变参数的终极写法**


public void method(Object... objects){//可以接受任意数据类型

}

posted @ 2020-08-11 17:59  DannyBoy~  阅读(181)  评论(0编辑  收藏  举报