Sorting and Re-arranging List of HashMaps
- by HonorGod
I have a List which is straight forward representation of a database table. I am trying to sort and apply some magic after the data is loaded into List of HashMaps. In my case this is the only hard and fast way of doing it becoz I have a rules engine that actually updates the values in the HashMap after several computations.
Here is a sample data representation of the HashMap (List of HashMap) -
{fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=21, toDate=Tue Mar 23 10:54:12 EDT 2010, actionId=1234}
{fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=456}
{fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=20, toDate=Thu Apr 01 10:54:12 EDT 2010, actionId=1234}
{fromDate=Wed Mar 24 10:54:12 EDT 2010, eventId=22, toDate=Sat Mar 27 10:54:12 EDT 2010, actionId=1234}
{fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Fri Mar 26 10:54:12 EDT 2010, actionId=1234}
{fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 31 10:54:12 EDT 2010, actionId=1234}
{fromDate=Mon Mar 15 10:54:12 EDT 2010, eventId=12, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=567}
I am trying to achieve couple of things -
1) Sort the list by actionId and eventId after which the data would look like -
{fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=456}
{fromDate=Mon Mar 15 10:54:12 EDT 2010, eventId=12, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=567}
{fromDate=Wed Mar 24 10:54:12 EDT 2010, eventId=22, toDate=Sat Mar 27 10:54:12 EDT 2010, actionId=1234}
{fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=21, toDate=Tue Mar 23 10:54:12 EDT 2010, actionId=1234}
{fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=20, toDate=Thu Apr 01 10:54:12 EDT 2010, actionId=1234}
{fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Fri Mar 26 10:54:12 EDT 2010, actionId=1234}
{fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 31 10:54:12 EDT 2010, actionId=1234}
2) If we group the above list by actionId they would be resolved into 3 groups - actionId=1234, actionId=567 and actionId=456. Now here is my question -
For each group having the same eventId, I need to update the records so that they have wider fromDate to toDate.
Meaning, if you consider the last two rows they have same actionId = 1234 and same eventId = 11. Now we can to pick the least fromDate from those 2 records which is Wed Mar 17 10:54:12 and farther toDate which is Wed Mar 31 10:54:12 and update those 2 record's fromDate and toDate to Wed Mar 17 10:54:12 and Wed Mar 31 10:54:12 respectively.
Any ideas?
PS: I already have some pseudo code to start with.
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.builder.CompareToBuilder;
public class Tester {
boolean ascending = true ;
boolean sortInstrumentIdAsc = true ;
boolean sortEventTypeIdAsc = true ;
public static void main(String args[]) {
Tester tester = new Tester() ;
tester.printValues() ;
}
public void printValues ()
{
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>() ;
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("actionId", new Integer(1234)) ;
map.put("eventId", new Integer(21)) ;
map.put("fromDate", getDate(1) ) ;
map.put("toDate", getDate(7) ) ;
list.add(map);
map = new HashMap<String,Object>();
map.put("actionId", new Integer(456)) ;
map.put("eventId", new Integer(11)) ;
map.put("fromDate", getDate(1)) ;
map.put("toDate", getDate(1) ) ;
list.add(map);
map = new HashMap<String,Object>();
map.put("actionId", new Integer(1234)) ;
map.put("eventId", new Integer(20)) ;
map.put("fromDate", getDate(4) ) ;
map.put("toDate", getDate(16) ) ;
list.add(map);
map = new HashMap<String,Object>();
map.put("actionId", new Integer(1234)) ;
map.put("eventId", new Integer(22)) ;
map.put("fromDate",getDate(8) ) ;
map.put("toDate", getDate(11)) ;
list.add(map);
map = new HashMap<String,Object>();
map.put("actionId", new Integer(1234)) ;
map.put("eventId", new Integer(11)) ;
map.put("fromDate",getDate(1) ) ;
map.put("toDate", getDate(10) ) ;
list.add(map);
map = new HashMap<String,Object>();
map.put("actionId", new Integer(1234)) ;
map.put("eventId", new Integer(11)) ;
map.put("fromDate",getDate(4) ) ;
map.put("toDate", getDate(15) ) ;
list.add(map);
map = new HashMap<String,Object>();
map.put("actionId", new Integer(567)) ;
map.put("eventId", new Integer(12)) ;
map.put("fromDate", getDate(-1) ) ;
map.put("toDate",getDate(1)) ;
list.add(map);
System.out.println("\n Before Sorting \n ");
for(int j = 0 ; j < list.size() ; j ++ )
System.out.println(list.get(j));
Collections.sort ( list , new HashMapComparator2 () ) ;
System.out.println("\n After Sorting \n ");
for(int j = 0 ; j < list.size() ; j ++ )
System.out.println(list.get(j));
}
public static Date getDate(int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, days);
return cal.getTime() ;
}
public class HashMapComparator2 implements Comparator
{
public int compare ( Object object1 , Object object2 )
{
if ( ascending == true )
{
return new CompareToBuilder()
.append(( ( HashMap ) object1 ).get ( "actionId" ), ( ( HashMap ) object2 ).get ( "actionId" ))
.append(( ( HashMap ) object2 ).get ( "eventId" ), ( ( HashMap ) object1 ).get ( "eventId" ))
.toComparison();
}
else
{
return new CompareToBuilder()
.append(( ( HashMap ) object2 ).get ( "actionId" ), ( ( HashMap ) object1 ).get ( "actionId" ))
.append(( ( HashMap ) object2 ).get ( "eventId" ), ( ( HashMap ) object1 ).get ( "eventId" ))
.toComparison();
}
}
}
}