1. 程式人生 > 其它 >集合(二)

集合(二)

Set集合

  • Set集合特點
    • 不包含重複元素的集合
    • 沒有索引的方法,所以不能使用普通for迴圈遍歷
  • Set集合練習
    • 儲存字元並遍歷
package Set;

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

/*
        Set集合特點:
                不包含重複元素的集合
                沒有索引的方法,所以不能使用普通的for迴圈遍歷

        HashSet: 對集合的迭代順序不做任何保證
*/
public class SetDemo {
    public static void
main(String[] args) { //建立集合物件 Set<String> set = new HashSet<String>(); //新增元素 set.add("hello"); set.add("world"); set.add("java"); //不包含重複元素的集合 set.add("world"); //遍歷 for (String s : set){ System.out.println(s); } } }

雜湊值

  • 雜湊值:是JDK根據物件的地址或者字串或者數字算出來的int型別的數值
  • Object中有一個方法可以獲得物件的雜湊值
    • public int hashCode(): 返回物件的雜湊碼值
  • 物件雜湊值特點
    • 同一個物件多次呼叫hashCode()方法返回的雜湊值是相同的。
    • 預設情況下,不同物件的雜湊值是不同的。而重寫的hashCode()方法,可以實現讓不同物件的雜湊值相同。
package Set;

public class HashDemo {
    public static void main(String[] args) {
        //建立學生物件
        Student s1 = new
Student("鍾啟航",21); //通一個物件呼叫多次hashCode的值是相同的 System.out.println(s1.hashCode());// 1163157884 System.out.println(s1.hashCode());// 1163157884 System.out.println("--------------------------"); //預設情況下,不同物件的雜湊值是不同的 //通過方法重寫,可以實現不同物件的雜湊值是相同的 Student s2 = new Student("蕪湖",21); System.out.println(s2.hashCode());//1956725890 System.out.println("---------------------------"); System.out.println("hello".hashCode());// 99162322 // System.out.println("hello".hashCode());// 99162322 System.out.println("world".hashCode());// 113318802 System.out.println("java".hashCode());// 3254818 System.out.println("---------------------------"); System.out.println("重地".hashCode());// 1179395 System.out.println("通話".hashCode());// 1179395 } }

HashSet集合概述和特點

  • HashSet集合特點
    • 底層資料結構是雜湊表
    • 對集合的迭代順序不作任何保證,也就是說不保證儲存和取出的元素順序一致
    • 沒有帶索引的方法,所以不能使用普通for迴圈遍歷
    • 由於是Set集合,所以是不包含重複元素的集合
  • HashSet集合練習
    • 儲存字串並遍歷
package Set;

import java.util.HashSet;

/*  `   HashSet集合特點
                底層資料結構是雜湊表
                對集合的迭代順序不作任何保證,也就是說不保證儲存和取出的元素順序一致
                沒有帶索引的方法,所以不能使用普通for迴圈遍歷
                由於是Set集合,所以是不包含重複元素的集合*/
public class HashSetDemo1 {
    public static void main(String[] args) {
        //建立集合物件
        HashSet<String> hs = new HashSet<String>();

        hs.add("hello");
        hs.add("world");
        hs.add("java");

        hs.add("world");

        for (String s : hs){
            System.out.println(s);
        }
    }
}

HashSet集合保證元素唯一性原始碼分析

  • HashSet集合新增一個元素的過程

HashSet集合儲存元素

  • 要保證元素唯一性,需要重寫hashCode()和equals()方法

常見資料結構之雜湊表

  • 雜湊表
    • JDK8 之前,底層採用陣列+連結串列實現,可以說是一個元素為連結串列的陣列
    • JDK8以後,在長度比較長的時候,底層實現了優化

HashSet集合儲存學生物件並遍歷

  • 需求: 建立一個儲存學生物件的集合,儲存多個學生物件,使用程式實現控制檯遍歷該集合
  • 要求: 學生物件的成員變數值相同,我們認為就是同一個物件
  • 思路
    • 1.定義學生類
    • 2.建立HashSet集合物件
    • 3.建立學生物件
    • 4.把學生新增到集合
    • 5.遍歷集合(增強for)
    • 最後補充:在Student類中重寫hashCode()和equals()方法。
public class HashDemo2 {
    public static void main(String[] args) {
        HashSet<Student> hs = new HashSet<Student>();

        Student s1 = new Student("蕪湖",21);
        Student s2 = new Student("uahu",22);
        Student s3 = new Student("wuhu",23);
        Student s4 = new Student("meih",22);

        Student s5 = new Student("meih",22);

        hs.add(s1);
        hs.add(s2);
        hs.add(s3);
        hs.add(s4);
        hs.add(s5);

        for (Student s : hs ){
            System.out.println(s.getName()+"--"+s.getAge());
        }

    }
}

注意:如何使資料具有唯一性?

  • 在Student類中重寫hashCode()和equals()方法。
package Set;

public 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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }
}