Create a collection of sub collection from a given Collection (Group By) in Java -


i have array list of dates spanning several years i.e. arraylist<date> datelist have object represents grid of months each year i.e.

public class datetilegrid {     private int mcurrentyear;     private arraylist<date> mdatetiles;         ... // getters , setters } 

i want create arraylist<datetilegrid> contain relevant dates each year.

i know how loop (nasty nested loops) hope there cleaner way achieve that.

probably need until java 8 closures comes

hashmap<integer, datetilegrid>  datagridmap = new hashmap<>(); (date date: datelist) {     int year = date.getyear(); //deprecated.. use better     datetilegrid datagrid = datagridmap.get(year);     if(datagrid == null){         datagrid = new datetilegrid();         datagrid.setcurrentyear(year);         datagrid.setdatetiles(new arraylist<date>());         datagridmap.put(year, datagrid);     }     datagrid.getdatetiles().add(date); } //here result arraylist<datetilegrid> result = new arraylist<>(datagridmap.values()); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -