HashSet(Hashmap)两道题

发布于 2021-08-12  93 次阅读


第一题:

package com.apesblog.day_24;

import java.util.ArrayList;
import java.util.HashSet;

public class Test1 {

    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Integer(1));
        arrayList.add(new Integer(2));
        arrayList.add(new Integer(3));
        arrayList.add(new Integer(1));
        System.out.println(arrayList);
        System.out.println(new ArrayList(new HashSet(arrayList)));

    }

}

第二题:

package com.apesblog.day_24;

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

public class Test2 {

    public static void main(String[] args) {
        HashSet<People> set = new HashSet<People>();
        People p1 = new People(001, "AA");
        People p2 = new People(002, "BB");
        set.add(p1);
        set.add(p2);
        System.out.println(set);
//      [People [id=1, name=AA], People [id=2, name=BB]]
        p1.name = "CC";
        set.remove(p1);
        System.out.println(set);
//      [People [id=1, name=CC], People [id=2, name=BB]]              
        set.add(new People(001, "CC"));
        System.out.println(set);
//      [People [id=1, name=CC], People [id=1, name=CC], People [id=2, name=BB]]       
        set.add(new People(001, "AA"));
        System.out.println(set);
//      [People [id=1, name=CC], People [id=1, name=CC], People [id=1, name=AA], People [id=2, name=BB]]

    }
}

class People {
    int id;
    String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        People other = (People) obj;
        return id == other.id && Objects.equals(name, other.name);
    }

    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + "]";
    }

    public People(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public People() {
        super();
        // TODO Auto-generated constructor stub
    }
}

HashMap同理,Set底层是Map

package com.apesblog.day_24;

import java.util.HashMap;
import java.util.Objects;

public class Test4 {

    public static void main(String[] args) {
        final String PRESENT = null;
        HashMap<People, String> hashMap = new HashMap<People, String>();
        People p1 = new People(100, "AA");
        hashMap.put(p1, PRESENT);
        System.out.println(hashMap);

        p1.name = "CC";
        hashMap.remove(p1);
        System.out.println(hashMap);

        hashMap.put(new People(100, "CC"), PRESENT);
        System.out.println(hashMap);

        hashMap.put(new People(100, "AA"), PRESENT);
        System.out.println(hashMap);

    }
}

class People {
    int id;
    String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        People other = (People) obj;
        return id == other.id && Objects.equals(name, other.name);
    }

    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + "]";
    }

    public People(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public People() {
        super();
        // TODO Auto-generated constructor stub
    }
}