Company and Employee : Comparator « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Collections Data Structure » ComparatorScreenshots 
Company and Employee

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class Company {
  public static void main(String args[]) {
    Employee emps[] new Employee("Finance""Degree, Debbie"),
        new Employee("Finance""Grade, Geri"),
        new Employee("Finance""Extent, Ester"),
        new Employee("Engineering""Measure, Mary"),
        new Employee("Engineering""Amount, Anastasia"),
        new Employee("Engineering""Ratio, Ringo"),
        new Employee("Sales""Stint, Sarah"),
        new Employee("Sales""Pitch, Paula"),
        new Employee("Support""Rate, Rhoda")};
    Set set = new TreeSet(Arrays.asList(emps));
    System.out.println(set);
    Set set2 = new TreeSet(Collections.reverseOrder());
    set2.addAll(Arrays.asList(emps));
    System.out.println(set2);

    Set set3 = new TreeSet(new EmpComparator());
    for (int i = 0, n = emps.length; i < n; i++) {
      set3.add(emps[i]);
    }
    System.out.println(set3);
  }
}

class EmpComparator implements Comparator {

  public int compare(Object obj1, Object obj2) {
    Employee emp1 = (Employeeobj1;
    Employee emp2 = (Employeeobj2;

    int nameComp = emp1.getName().compareTo(emp2.getName());

    return ((nameComp == 0? emp1.getDepartment().compareTo(
        emp2.getDepartment()) : nameComp);
  }
}

class Employee implements Comparable {
  String department, name;

  public Employee(String department, String name) {
    this.department = department;
    this.name = name;
  }

  public String getDepartment() {
    return department;
  }

  public String getName() {
    return name;
  }

  public String toString() {
    return "[dept=" + department + ",name=" + name + "]";
  }

  public int compareTo(Object obj) {
    Employee emp = (Employeeobj;
    int deptComp = department.compareTo(emp.getDepartment());

    return ((deptComp == 0? name.compareTo(emp.getName()) : deptComp);
  }

  public boolean equals(Object obj) {
    if (!(obj instanceof Employee)) {
      return false;
    }
    Employee emp = (Employeeobj;
    return department.equals(emp.getDepartment())
        && name.equals(emp.getName());
  }

  public int hashCode() {
    return 31 * department.hashCode() + name.hashCode();
  }
}

           
       
Related examples in the same category
1. Creating a Comparable objectCreating a Comparable object
2.  Writing Your own Comparator Writing Your own Comparator
3. A Class Implementing Comparable
4. Comparator for comparing strings ignoring first character
5. Customized Sort Test
6. List and Comparators
7. Sort backwards
8. Search with a Comparator
9. Keep upper and lowercase letters togetherKeep upper and lowercase letters together
10. Uses anonymous inner classesUses anonymous inner classes
11. Building the anonymous inner class in-placeBuilding the anonymous inner class in-place
w__w__w_.___j__a__v_a_2__s.c___o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.