Monday, November 05, 2007

segregating objects as per code value in a map

We fetch details from a table which has say 5 columns and populate it inside a look-up bean(ABCBean) object and add it to an arraylist(ABCList).

It has columns like display(the string to be displayed to the user), code (which is a not a unique value and helps in grouping the display values to be shown on screen).

Example: It has codes like screen-1, screen-2. For screen-1 and 2 we have multiple display values in database table.

display code
a screen-1
b screen-1
c screen-1
a screen-2
c screen-2
e screen-2

Now all these display values are in a single arraylist(ABCList). We now need to segregate these values as per the code and put it in a map.
The below code seperates and stores all the bean objects as per the code value inside a map.

public void segregateMethod(ABCList abcList){
Map abcMap = new TreeMap();
if(abcList != null) {
for(Iterator itr = abcList.iterator();itr.hasNext();) {
ABCBean abcBean = (ABCBean) itr.next();
if(abcBean != null) {
String code = abcBean.getCode();
List abcValues = (List)abcMap.get(code);
if(abcValues == null) abcValues = new ArrayList();
abcValues.add(abcBean);
abcMap.put(code, abcValues);
}
}
}
}

0 Comments:

Post a Comment

<< Home