Sunday, February 26, 2012

Sorting a Map using key objects

package collectionFramework;

import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;

/**
* The Class HashMapAddSearchSort.
*/
public class HashMapAddSearchSort
{

/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args)
{
HashMap map= new HashMap();
map.put(new Employee("abhinav",21,24000),"INNODATA");
map.put(new Employee("abhishek",25,23000),"AIRTEL");
map.put(new Employee("ashutosh",22,22000),"VODAFONE");
map.put(new Employee("rinku",24,25000),"HCL");
map.put(new Employee("rinku",24,25000),"HCL");
System.out.println("Search for value on basis of key: >"+map.containsKey(new Employee("ashutosh",22,22000)));
System.out.println("Search for value 'INNODATA' :> "+map.containsValue("INNODATA"));
System.out.println("UNSORTED: "+map);

/*TreeMap sortedMap = new TreeMap();
sortedMap.putAll(map);
System.out.println("SORTED USING AGE-BASED-COMPARABLE: "+sortedMap);*/

TreeMap sortedMapNameComparator = new TreeMap(new NameComparator());
sortedMapNameComparator.putAll(map);
System.out.println("SORTED USING NAME-COMPARATOR: "+sortedMapNameComparator);

TreeMap sortedMapAgeComparator = new TreeMap(new AgeComparator());
sortedMapAgeComparator.putAll(map);
System.out.println("SORTED USING AGE-COMPARATOR: "+sortedMapAgeComparator);

TreeMap sortedMapSalaryComparator = new TreeMap(new SalaryComparator());
sortedMapSalaryComparator.putAll(map);
System.out.println("SORTED USING SALARY-COMPARATOR: "+sortedMapSalaryComparator);
}
}


/**
* The Class AgeComparator.
*/
class AgeComparator implements Comparator
{
public int compare(Employee o1,Employee o2){
if(o1.age>o2.age){
return 1;
}else if(o1.age
{
public int compare(Employee o1,Employee o2){
return o1.name.compareTo(o2.name);
}
}


/**
* The Class SalaryComparator.
*/
class SalaryComparator implements Comparator
{
public int compare(Employee o1,Employee o2){
if(o1.salary>o2.salary){
return 1;
}else if(o1.salary
{

/** The name. */
String name;

/** The age. */
int age;

/** The salary. */
int salary;

/**
* Instantiates a new employee.
*/
public Employee() {
super();
}

/**
* Instantiates a new employee.
*
* @param name the name
* @param age the age
* @param salary the salary
*/
public Employee(String name, int age, int salary)
{
super();
this.name = name;
this.age = age;
this.salary = salary;
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
return "\nEmployeeDetails:>> ( "+name+" :: "+age+" :: "+salary+" )";
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
int result = 1;
return result;
}

/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o)
{
Employee e=(Employee)o;
if(this.name.hashCode() ==e.name.hashCode() && this.age==e.age && this.salary==e.salary)
{
return true;
}
return false;
}

/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Employee o)
{
//return this.name.compareToIgnoreCase(o.name);

/*if(this.salary>o.salary){
return 1;
}else if(this.salaryo.age){
return 1;
}else if(this.age

No comments:

Post a Comment

Thanks for your comments/Suggestions.