Tuesday, April 8, 2008

HashMap Sorting Service



package com.girnarsoft.services;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class HashMapSortingService
{
public void getSort(HashMap m)
{
ArrayList outputList = sortMap(m);
int count = 0;
count = outputList.size();
System.out.println("Sorted List is as follows");
while(count > 0)
{
Map.Entry entry = (Map.Entry) outputList.get(--count);
System.out.println("Key:- "+entry.getKey()+"Value:- "+entry.getValue());
}
}

/**
* This method will use Arrays.sort for sorting Map
* @param map
* @return outputList of Map.Entries
*/
public ArrayList sortMap(Map map)
{
ArrayList outputList = null;
int count = 0;
Set set = null;
Map.Entry[] entries = null;
// Logic:
// get a set from Map
// Build a Map.Entry[] from set
// Sort the list using Arrays.sort
// Add the sorted Map.Entries into arrayList and return

set = (Set) map.entrySet();
Iterator iterator = set.iterator();
entries = new Map.Entry[set.size()];
while(iterator.hasNext()) {
entries[count++] = (Map.Entry) iterator.next();
}

// Sort the entries with your own comparator for the values:
Arrays.sort(entries, new Comparator()
{
public int compareTo(Object lhs, Object rhs) {
Map.Entry le = (Map.Entry)lhs;
Map.Entry re = (Map.Entry)rhs;
return ((Comparable)le.getValue()).compareTo((Comparable)re.getValue());
}

public int compare(Object lhs, Object rhs) {
Map.Entry le = (Map.Entry)lhs;
Map.Entry re = (Map.Entry)rhs;
return ((Comparable)le.getValue()).compareTo((Comparable)re.getValue());
}
});
outputList = new ArrayList();
for(int i = 0; i < entries.length; i++) {
outputList.add(entries[i]);
}
return outputList;
}

}

No comments:

 

blogger templates | Make Money Online