Java/Comparators

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Java Data Types, Variables,and Arrays
Java Operators
Java Packages, Classes, and Interfaces
Java Program
Java Modifiers
Java Control Statements
Java Exception Handling
Java Object Oriented Programming
Java Wrappers
Java Strings
Java Math
Java Arrays
Java Random Numbers
Java Date and Time
Java Regular Expressions
Java Collections
* Java List
* Java Set
* Java Comparators
* Java Iterators
* Java Map
* Java Legacy Classes and Interfaces
Java Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
Java RMI
Java Image, Audio, Video
Java GUI
Java Applets
Java Internationalization
JDBC
Java and XML

[edit] Java Comparators

Comparator actually gives a precise understanding of the term “sorted Order” It basically stores the element in exactly the order which is expected to be in sense the natural sorting order. This method defines two other methods compare( ) and equals ( ). Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).

The ordering imposed by a Comparator c on a set of elements S is said to be consistent with equals if and only if (compare((Object)e1, (Object)e2)==0) has the same boolean value as e1.equals((Object)e2) for every e1 and e2 in S.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
 
public class ComparatorExample {
 
	public static void main(String[] args) {
 
		List employee = new ArrayList();
		employee.add(new ComparatorExample().new Employee(2, "Sam", 10000));
		employee.add(new ComparatorExample().new Employee(4, "John", 20000));
		employee.add(new ComparatorExample().new Employee(1, "Rohan", 30000));
		employee.add(new ComparatorExample().new Employee(3, "Willey", 40000));
 
		Collections.sort(employee, new ComparatorExample().new Employee());
 
		for (Iterator iter = employee.iterator(); iter.hasNext();) {
			Employee emp = (Employee) iter.next();
			System.out.println(emp.toString());
		}
	}
 
	class Employee implements Comparator {
 
		private int id;
 
		private String name;
 
		private long sal;
 
		public Employee() {
 
		}
 
		public Employee(int id, String name, long sal) {
 
			this.id = id;
			this.name = name;
			this.sal = sal;
		}
 
		public int compare(Object obj1, Object obj2) {
 
			Employee employee1 = (Employee) obj1;
			Employee employee2 = (Employee) obj2;
 
			if (employee1.id < employee2.id) {
				return -1;
			}
 
			if (employee1.id > employee2.id) {
				return 1;
			}
 
			return 0;
		}
 
		public boolean equals(Object o) {
			if (o instanceof Employee) {
				return false;
			}
 
			Employee employee = (Employee) o;
 
			if (employee.id == this.id) {
				return true;
			}
 
			return true;
		}
 
		public String toString() {
			return "id: " + this.id + "\n" + "Name: " + this.name + "\n"
					+ "Sal: " + this.sal;
		}
	}
 
}


Output

id: 1
Name: Rohan
Sal: 30000
id: 2
Name: Sam
Sal: 10000
id: 3
Name: Willey
Sal: 40000
id: 4
Name: John
Sal: 20000