1. 程式人生 > 實用技巧 >vector容器

vector容器

技術標籤:Javajava

程式碼1:

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

/*
    單列集合(Collection)之Set集合:
        特點:
            無序(元素的存取順序不一致), 唯一.

        結論:
            Set集合保證元素的唯一性依賴: equals(), hashCode()兩個方法.
 */
public class SetTeat {
    public static void main(String[] args) {
//需求: 往set集合中新增5個學生物件, 然後遍歷.
//        1.建立集合物件
        Set<Student> set=new HashSet();
//        2.建立元素物件
        Student s1=new Student("喬峰",40);
        Student s2=new Student("段譽",20);
        Student s3=new Student("虛竹",30);
        Student s4=new Student("虛竹",30);
        Student s5=new Student("喬峰",40);
        Student s6=new Student("段譽",20);

//        3.將集合物件新增到元素物件中

        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);

//        4.遍歷集合
        System.out.println(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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

列印結果

無序但似乎並沒有唯一,

為什麼Set集合沒有"去重"(其實已經去重)?
因為Set集合保證元素的唯一性依賴: equals() 和 hashCode()兩個方法.
你沒有在Student類中重寫這兩個方法, 預設呼叫的是Object類中的這兩個方法.
而Object類中的equals()方法預設比較的是地址值是否相同.

解決方案:
在Student類中重寫equals()和hashCode()方法.

把Student類改為

import java.util.Objects;

//學生類
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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//重寫equals()和hashCode()
    @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);
    }
}

列印結果

修改主程式,通過遍歷集合的方式列印結果:

程式碼2:

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


public class SetTeat {
    public static void main(String[] args) {
//需求: 往set集合中新增5個學生物件, 然後遍歷.
//        1.建立集合物件
        Set<Student> set=new HashSet();
//        2.建立元素物件
        Student s1=new Student("喬峰",40);
        Student s2=new Student("段譽",20);
        Student s3=new Student("虛竹",30);
        Student s4=new Student("虛竹",30);
        Student s5=new Student("喬峰",40);
        Student s6=new Student("段譽",20);

//        3.將集合物件新增到元素物件中

        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);

//        4.遍歷集合
        System.out.println(set);

        System.out.println("通過迭代器遍歷集合");
//        通過迭代器遍歷集合
        //A. 通過集合物件獲取其對應的迭代器物件.
        Iterator<Student> it=set.iterator();

        //B. 判斷迭代器中是否有元素.
        while(it.hasNext()){
            //C. 如果有, 就獲取元素.
            Student s=it.next();
            System.out.println(s);
        }

        System.out.println("通過增強for迴圈遍歷集合");
//        通過增強for迴圈遍歷集合
        for (Student student : set) {
            System.out.println(student);
            
        }
        
    }
}
import java.util.Objects;

//學生類
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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//重寫equals()和hashCode()
    @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);
    }
}
列印結果