java - Convert list to map -


using java 1.5 how can convert following list structure nested map:

list<cssectiondetail> list map<integer,map<integer, list<cssectiondetail>>> map = new treemap<integer, map<integer,list<cssectiondetail>>>();

the above list contains 2 keys maintaskid , subtaskid should converted above map structure first integer maintaskid , second integer subtaskid.

i have function convert list<cssectiondetail> list map , structure map<integer, list<cssectiondetail>> map = new hashmap<integer, list<cssectiondetail>>(); below code

public map<integer, list<cssectiondetail>> getcssectiondetail4display(list<cssectiondetail> list, boolean groupbymaintaskonly) {         map<integer, list<cssectiondetail>> map = new hashmap<integer, list<cssectiondetail>>();          iterator<cssectiondetail> = list.iterator();     while(it.hasnext()){             cssectiondetail csdetail = (cssectiondetail) it.next();             add2map(csdetail, map, groupbymaintaskonly);     }     return map;     }  private void add2map(cssectiondetail csdetail, map<integer, list<cssectiondetail>> map, boolean groupbymaintaskonly) {        list<cssectiondetail> list;         integer key;           if (groupbymaintaskonly) {             key = csdetail.getprojecttaskid().getid();         } else {             if (csdetail.getprojectsubtaskid() == 0) {                 key = csdetail.getprojecttaskid().getid();             } else {                 key = new integer(csdetail.getprojectsubtaskid());             }         }         list = (list<cssectiondetail>) map.get(key);         if (list == null) {             list = new arraylist<cssectiondetail>();         } 

how can modify above code nested map structure?

thanks

cut code in 2 methods clarity, each filling map @ different level:

private void add2taskmap(cssectiondetail csdetail, map<integer, map<integer,cssectiondetail>> map) {     map<integer,cssectiondetail> submap;      integer key = csdetail.getprojecttaskid().getid();       submap =map.get(key);      if (submap == null) {          submap = new hashmap<integer,cssectiondetail>();          map.put(key, submap);      }      add2subtaskmap(csdetail, submap); }  private void add2subtaskmap(cssectiondetail csdetail, map<integer,cssectiondetail> map) {      integer key;      if (csdetail.getprojectsubtaskid() == 0) {          key = csdetail.getprojecttaskid().getid();      } else {          key = new integer(csdetail.getprojectsubtaskid());      }       map.put(key, csdetail); } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -