A map contains values as bean's object, if one wants to sort map based on the value of the bean's property.
Following code snippet demonstrate the way to sort a map based on the value having bean object having property as name. There is a bean named TestBean having properties like name, age and address. A map contains value as object of TestBean. This map is sorted based on the value of bean's name property.
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class BeanUtility{
public static void main(String [] args){
//Creating map object.
Map mapObj=new HashMap();
//Create bean object and put it into the map.
TestBean bean=new TestBean();
bean.setName("Bac");
bean.setAge(25);
bean.setAddress("delhi");
mapObj.put("one", bean);
TestBean bean2=new TestBean();
bean2.setName("Cab");
bean2.setAge(24);
bean2.setAddress("NCR");
mapObj.put("two", bean2);
TestBean bean3=new TestBean();
bean3.setName("Aac");
bean3.setAge(23);
bean3.setAddress("Gurgaon");
mapObj.put("three", bean3);
//Sort Map
Map sortedMap=sortMapByValue(mapObj);
//Print Map Values
printMap(sortedMap);
}
// Method to sort Map.
private static Map<String, TestBean> sortMapByValue(Map<String, TestBean> unsortMap) {
//Convert Map to LinkList
List<Map.Entry<String, TestBean>> listObj = new LinkedList<Map.Entry<String, TestBean>> (unsortMap.entrySet());
// Sort list with comparator
Collections.sort(listObj, new Comparator<Map.Entry<String, TestBean>>() {
public int compare(Map.Entry<String, TestBean> object1, Map.Entry<String, TestBean> object2) {
//compare name property's value.
return ((TestBean) object1.getValue()).getName().compareTo(((TestBean) object2.getValue()).getName());
}
});
//Fill sorted list into LinkedHashMap.
Map<String, TestBean> objLinkHashMap = new LinkedHashMap<String, TestBean>();
for (Iterator<Map.Entry<String, TestBean>> itrator = listObj.iterator(); itrator.hasNext();) {
Map.Entry<String, TestBean> entry = itrator.next();
objLinkHashMap.put(entry.getKey(), entry.getValue());
}
return objLinkHashMap;
}
// Method to print Map Values.
private static void printMap(Map objMap){
Collection<TestBean> collection=objMap.values();
Iterator<TestBean> itr=collection.iterator();
while(itr.hasNext()){
TestBean bean=itr.next();
System.out.println("name: "+bean.getName()+" Address: "+bean.getAddress()+" age: "+bean.getAge());
}
}
}
// Bean
class TestBean{
private String name ;
private String address;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public TestBean(){
}
}
OUTPUT:
Following code snippet demonstrate the way to sort a map based on the value having bean object having property as name. There is a bean named TestBean having properties like name, age and address. A map contains value as object of TestBean. This map is sorted based on the value of bean's name property.
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class BeanUtility{
public static void main(String [] args){
//Creating map object.
Map mapObj=new HashMap();
//Create bean object and put it into the map.
TestBean bean=new TestBean();
bean.setName("Bac");
bean.setAge(25);
bean.setAddress("delhi");
mapObj.put("one", bean);
TestBean bean2=new TestBean();
bean2.setName("Cab");
bean2.setAge(24);
bean2.setAddress("NCR");
mapObj.put("two", bean2);
TestBean bean3=new TestBean();
bean3.setName("Aac");
bean3.setAge(23);
bean3.setAddress("Gurgaon");
mapObj.put("three", bean3);
//Sort Map
Map sortedMap=sortMapByValue(mapObj);
//Print Map Values
printMap(sortedMap);
}
// Method to sort Map.
private static Map<String, TestBean> sortMapByValue(Map<String, TestBean> unsortMap) {
//Convert Map to LinkList
List<Map.Entry<String, TestBean>> listObj = new LinkedList<Map.Entry<String, TestBean>> (unsortMap.entrySet());
// Sort list with comparator
Collections.sort(listObj, new Comparator<Map.Entry<String, TestBean>>() {
public int compare(Map.Entry<String, TestBean> object1, Map.Entry<String, TestBean> object2) {
//compare name property's value.
return ((TestBean) object1.getValue()).getName().compareTo(((TestBean) object2.getValue()).getName());
}
});
//Fill sorted list into LinkedHashMap.
Map<String, TestBean> objLinkHashMap = new LinkedHashMap<String, TestBean>();
for (Iterator<Map.Entry<String, TestBean>> itrator = listObj.iterator(); itrator.hasNext();) {
Map.Entry<String, TestBean> entry = itrator.next();
objLinkHashMap.put(entry.getKey(), entry.getValue());
}
return objLinkHashMap;
}
// Method to print Map Values.
private static void printMap(Map objMap){
Collection<TestBean> collection=objMap.values();
Iterator<TestBean> itr=collection.iterator();
while(itr.hasNext()){
TestBean bean=itr.next();
System.out.println("name: "+bean.getName()+" Address: "+bean.getAddress()+" age: "+bean.getAge());
}
}
}
// Bean
class TestBean{
private String name ;
private String address;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public TestBean(){
}
}
OUTPUT:
No comments:
Post a Comment