كل عام والاخوة بالف الف خير
صراحة اثناء تصفحي للمنتدى لفت انتباهي سؤال لاحد الاخوة عن مشكلة تواجهه وبالمصادفة انني ابحث عن الحل لها ولم اجد وسأضع المثال الذي طرحه الاخ في الرابط /index.php/topic/279857-%D8%B9%D9%85%D9%84-%D9%83%D8%A7%D8%A6%D9%86-%D9%85%D9%86-%D8%B5%D9%86%D9%81-%D9%81%D9%8A-%D8%B5%D9%86%D9%81-%D8%A3%D8%AE%D8%B1/
public class Employee {
protected static int id;
protected static String name;
protected static String address;
protected static double salary;
public Employee(int id, String name, String address, double salary) {
this.id = id;
this.name = name;
this.address = address;
this.salary = salary;
}
//sette and getter
public String toString(){
return "ID:" + getId() + ", Name:" + getName() + ", Address:" + getAddress() + ", Salary:" + getSalary();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, "Ali", "Aleppo", 200.0);
Employee ts2 = new Employee (11, "Amal", "Berlin", 600.0);
Employee ts3 = new Employee (12, "Salim", "Homs", 250.0);
Employee ts4 = new Employee (10, "Ali", "Aleppo", 200.0);
Employee ts5 = new Employee (14, "yaser", "Damas", 210.0);
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);
}
}
}طبعا هنا لن سوف يقوم بطباعة جميع العناصر الا اذا قمنا بتعريف الدوال equal and hashcode
@Override
public boolean equals(Object otherObject) {
if ( otherObject == this )
return true;
if ( otherObject == null || !(otherObject instanceof Employee))
return false;
Employee other = (Employee) otherObject;
return other.id == this.id;
}
@Override
public int hashCode() {
return id;
}الآن أعود واطرح نفس سؤال الاخ ماذا لو احببت ان اعيد تعريف الدوال equal and hashcode بحيث يشمل كل من الاسم والرقم او الاسم والعنوان والرقم المهم اكثر من عنصر فكيف نعمل ذلك؟ ارجو المساعدة