HashSet speichert doppelte werte

vector_ever

Mitglied
Hallo,

Bekanntlich dass HashSet keine doppelte werte annimmt, aber was ist in Fall mit Objekten?

Code:
import java.util.Comparator;
 
public class Employee{
 
    private int id;
    private String name;
    private String address;
    private double salary;
 
    public Employee(int id, String name, String address, double salary) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.salary = salary;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    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;
    }
 
    public double getSalary() {
        return salary;
    }
 
    public void setSalary(double salary) {
        this.salary = salary;
    }
 
    public String toString(){
        return "ID:" + getId() + ", Name:" + getName() + ", Address:" + getAddress() + ", Salary:" + getSalary();
    }
            }

erzeuge Objekten und addieren in ArrayList und HashList
Code:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
 
 
public class Main {
 
    public static void main(String args[]) {
        Employee ts1 = new Employee (10, "Sam", "Paris", 200.0);
        Employee ts2 = new Employee (11, "Amal", "Berlin", 600.0);
        Employee ts3 = new Employee (12, "Nik", "London", 250.0);
        Employee ts4 = new Employee (10, "Sam", "Paris", 200.0);
        Employee ts5 = new Employee (14, "Jasmin", "Damas", 210.0);
 
        ArrayList<Employee> list = new ArrayList<Employee>();
 
        list.add(ts1);
        list.add(ts2);
        list.add(ts3);
        list.add(ts4);
        list.add(ts5);
 
        System.out.println("List");
        System.out.println("===================================================");
        for (int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }
 
        Set<Employee> set = new HashSet<Employee>();
 
        set.add(ts1);
        set.add(ts2);
        set.add(ts3);
        set.add(ts4);
        set.add(ts5);
 
        System.out.println("");
        System.out.println("Set");
        System.out.println("===================================================");
    for(Employee item: set){
         System.out.println(item);
    }
    }
}

Ausgabe:
HTML:
List
===================================================
ID:10, Name:Sam, Address:Paris, Salary:200.0
ID:11, Name:Amal, Address:Berlin, Salary:600.0
ID:12, Name:Nik, Address:Lndon, Salary:250.0
ID:10, Name:Sam, Address:Paris, Salary:200.0
ID:14, Name:Jasmin, Address:Damas, Salary:210.0
 
Set
===================================================
ID:12, Name:Nik, Address:London, Salary:250.0
ID:10, Name:Sam, Address:Paris, Salary:200.0
ID:14, Name:Jasmin, Address:Damas, Salary:210.0
ID:10, Name:Sam, Address:Paris, Salary:200.0
ID:11, Name:Amal, Address:Berlin, Salary:600.0

es schaut mal, keine Unterschied zwischen HashList und ArrayList (nur andere Sortierung)

Also wie kann ich diese code bearbeiten, damit HashSet nimmt keine doppelte werte an
 
Code:
Du mußt entsprechende Hashcodes für deine Objekte vergeben, welche von den Eigenschaften deiner Klasse abhängen.

Siehe https://en.wikipedia.org/wiki/Java_hashCode%28%29

ich rede nicht über Hasehn, meine frage geht um Set und HashSet ist interface von Set

Außerdem brauchst du auch noch eine ordentliche Vergleichsmöglichkeit, d.h. du mußt equals überschreiben.

ok aber wie kann ich so machen, damit habe ich keine Erfahrung
 
Ich habe diese Teil in Employee.java Class hinzugefügt und es funktioniert sehr gut

Code:
    @Override
   public boolean equals(Object obj){  
      
        if(!(obj instanceof Employee))  
            return false;  
          
        return (id == ((Employee) obj).getId());   
    }  
      
   
    @Override
    public int hashCode(){  
            
        return  id;    
    }

Aber was möchte ich auch, wie kann mann (beim equal() Methode) mehre Attributen vergleichen, nicht nur ein.

Ich meine in meine Lösung, habe ich nur mit dem (id) vergleicht, aber ich möchte auch mehr
Z.B id, name und address

wie kann ich so machen, was soll ich in equal() Methode noch schreiben?
 
Du kannst dir über deine Entwicklungsumgebung z.B. Eclipse, die Methoden "equals" und "hashcode" erzeugen lassen. Dann hast du auch eine vernünftige Implementierung. Weil er auch null Werte berücksichtigt.
 
Zurück