2011-08-19 28 views
6

अगर मैं निम्नलिखित वर्ग है:जावा तुलनित्र का सही तरीके से उपयोग कैसे करें?

public class Employee { 
    private int empId; 
    private String name; 
    private int age; 

    public Employee(int empId, String name, int age) { 
     // set values on attributes 
    } 
    // getters & setters 
} 

मैं तुलनित्र का उपयोग कैसे कर सकते हैं कि नाम है, तो उम्र है, तो आईडी से तुलना?

+2

, कृपया कह क्यों/कैसे सवाल आप के लिए एक downvote लायक द्वारा मदद करते हैं। –

+0

क्या आप ".equals()" विधि को ओवरराइड नहीं कर सकते हैं जो सभी तीन मानों की तुलना करता है? ... ओह क्षमा करें, मुझे थोड़ी देर बाद एहसास हुआ कि आप ऑब्जेक्ट्स को सॉर्ट करना चाहते हैं और इस प्रकार तुलनित्र! पुराने प्रश्नों की खोज के लिए – Sap

उत्तर

8

आपको इसे लागू करने की आवश्यकता है ताकि यह पसंदीदा तत्वों द्वारा ऑर्डर कर सके।

public class EmployeeComparator implements Comparator<Employee> { 

    @Override 
    public int compare(Employee e1, Employee e2) { 
    int nameDiff = e1.getName().compareTo(e2.getName()); 

    if(nameDiff != 0) { 
     return nameDiff; 
    } 

    int ageDiff = e1.getAge() - e2.getAge(); 

    if(ageDiff != 0) { 
     return ageDiff; 
    } 

    int idDiff = e1.getEmpId() - e2.getEmpId(); 

    return idDiff; 
    } 
} 
+3

जो कोई भी अनाम डाउनवोट करता है, क्या आप कृपया सलाह दे सकते हैं कि मैं अपना जवाब कैसे सुधार सकता हूं, या कम से कम इसमें क्या गलत है? धन्यवाद। – Bringer128

4

अद्यतन

एक पल पहले इस भर में आया: How to compare objects by multiple fieldsComparatorChain जो एक गैर शून्य परिणाम जब तक अनुक्रम में एक से अधिक तुलनाकारक लागू करेगा से जुड़ा हुआ जवाब से एक एक तुलनित्र से प्राप्त होता है या सभी तुलनाकारक लागू कर रहे हैं। यह शायद आपका पसंदीदा समाधान होना चाहिए।


शायद यह (untested) Comparator#compare() के कार्यान्वयन चाल करेंगे।

int compare(Employee e, Employee f) 
{ 
    int val = e.name.compareTo(f.name); 

    if(val == 0) 
    { 
     val = e.age - f.age; 

     if(val == 0) 
     { 
      val = e.empId - f.empId; 
     } 
    } 

    return val; 
} 
+0

+1। (और एक गुणवत्ता का जवाब) – Bringer128

1

आप भी अपनी कक्षा में तुलनीय इंटरफ़ेस लागू कर सकते हैं: यह आप है, तो अगर उस तुलना बराबर है, उम्र से तुलना, आदि नाम से तुलना करने की आवश्यकता एक उदाहरण नीचे सूचीबद्ध है, है।

उदाहरण के लिए

, कुछ इस तरह:

public class Employee implements Comparable<Employee>{ 
    private int empId; 
    private String name; 
    private int age; 

    public Employee(int empId, String name, int age) { 
      // set values on attributes 

    } 
    // getters & setters 

    public int compareTo(Employee o) { 
     int ret = this.name.compareTo(o.name); 
     if(ret == 0) 
      ret = this.age - o.age; 
     if(ret == 0) 
      ret = this.empId - o.empId; 

     return ret; 
    } 
} 

तो आप अपने कर्मचारियों की तुलना करने के लिए एक अतिरिक्त वर्ग को लागू करने के लिए नहीं है।

1

लागू यह

public class Employee { 
    private int empId; 
    private String name; 
    private int age; 
    /** 
    * @param empId 
    * @param name 
    * @param age 
    */ 
    public Employee(int empId, String name, int age) { 
     super(); 
     this.empId = empId; 
     this.name = name; 
     this.age = age; 
    } 
    /** 
    * 
    */ 
    public Employee() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 


    public int getEmpId() { 
     return empId; 
    } 
    public void setEmpId(int empId) { 
     this.empId = empId; 
    } 
    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; 
    } 

    //Compare by name, age and then id 
    public static Comparator<Employee> COMPARE_EMPLOYEE = new Comparator<Employee>() { 
     public int compare(Employee one, Employee other) { 
      //Compare Name 
      if (one.getName().compareToIgnoreCase(other.getName()) == 0) { 
       //Compare age 
       if((one.getAge() - other.getAge()) == 0) { 
        // Now check with id is useless 
        // So directly return result of compare by id 
        return one.getEmpId() - other.getEmpId(); 
       } else { //If age Not equal 
        return one.getAge() - other.getAge(); 
       } 
      } else { //If name not equal 
       return one.getName().compareToIgnoreCase(other.getName()); 
      } 
     } 
    }; 
} 

उपयोग:

List<Employee> contacts = new ArrayList<Employee>(); 
//Fill it. 

//Sort by address. 
Collections.sort(contacts, Employee.COMPARE_EMPLOYEE); 

पढ़ें Sorting an ArrayList of Contacts, यह आप मदद करनी चाहिए और आप और अधिक विचारों और तुलनाकारी के उपयोग के विभिन्न विभिन्न प्रकार के मिल जाएगा।

1

अमरूद ComparisonChain:

List<Employee> list = new ArrayList<Employee>(); 
    //... 
    Collections.sort(list, new Comparator<Employee>(){  
     @Override 
     public int compare(Employee e1, Employee e2) { 
      return ComparisonChain.start() 
       .compare(e1.empId, e2.empId) 
       .compare(e1.name, e2.name) 
       .compare(e1.age, e2.age).result(); 
    }}); 
0

इस का उपयोग करें: उन downvoting लिए

public class Test 
{ 
    public static void main(String[] args) 
    { 
     Employee emp1 = new Employee(2, "Tom", 20); 
     Employee emp2 = new Employee(1, "Tom", 20); 
     Employee emp3 = new Employee(3, "Hank", 21); 

     List<Employee> list = new ArrayList<>(); 

     list.add(emp1); 
     list.add(emp2); 
     list.add(emp3); 

     Collections.sort(list, new Employee().new MyComparator()); 

     System.out.println(list); 
    } 
} 

class Employee 
{ 
    private int empId; 
    private String name; 
    private int age; 

    public Employee() 
    {} 

    public Employee(int empId, String name, int age) 
    { 
     this.empId = empId; 
     this.name = name; 
     this.age = age; 
    } 

    class MyComparator implements Comparator<Employee> 
    { 
     @Override 
     public int compare(Employee e1, Employee e2) 
     { 
      if(e1.name.compareTo(e2.name) == 0) 
      { 
       if(((Integer)e1.age).compareTo(e2.age) == 0) 
       { 
        return ((Integer)e1.empId).compareTo(e2.empId); 
       } 
       else 
       { 
        return ((Integer)e1.age).compareTo(e2.age); 
       } 
      } 
      return e1.name.compareTo(e2.name); 
     } 
    } 

    @Override 
    public String toString() 
    { 
     return "Employee [empId=" + empId + ", name=" + name + ", age=" + age + "]"; 
    } 
} 
संबंधित मुद्दे