Note that there are some explanatory texts on larger screens.

plurals
  1. POunable to group some values based on some condition
    text
    copied!<p>I have some values return from database as a result set like following</p> <pre><code>**resource_name menu_name menu_group_name** DepartmentAction Department Admin Operation PositionAction Position Admin Operation FoodHabitAction FoodHabits Admin Operation ReligiousAction Religious Admin Operation NationalitiesAction Nationlities Admin Operation </code></pre> <p>I would like to group resource_name and menu_name based on menu_group_name some thing like the following</p> <p>If the menu_group_name is same than group all the corresponding resource_name and menu_mane against to menu_group_name.</p> <p>This is the method i tried,</p> <pre><code>public Map&lt;String,List&gt; getMenuForLoggedinRole(int roleid){ Map&lt;String,List&gt; menuListMap = new LinkedHashMap&lt;String,List&gt;(); List&lt;MenuListViewModel&gt; menuNamesList = new ArrayList&lt;MenuListViewModel&gt;(); MenuListViewModel menuViewModel; Connection connection = getConnection(); PreparedStatement ps = null; ResultSet rs = null; if (connection != null) { try { ps = connection.prepareStatement(" select ar.resource_name,ar.menu_name,mg.menu_group_name " + " from m_application_resources as ar," + " m_menu_groups as mg,m_access_matrix as amatrix " + " where ar.resourceid = amatrix.resourceid and amatrix.roleid=?"); ps.setInt(1, roleid); rs = ps.executeQuery(); if(rs.next()) { String menu_group_name = rs.getString("menu_group_name"); String resource_name = rs.getString("resource_name"); String menu_name = rs.getString("menu_name"); if(menuListMap.containsKey(menu_group_name)){ menuNamesList =(List) menuListMap.get(menu_group_name); menuViewModel = new MenuListViewModel(); menuViewModel.setResource_name(resource_name); menuViewModel.setMenu_name(menu_name); menuNamesList.add(menuViewModel); menuListMap.put(menu_group_name, menuNamesList); }else{ menuViewModel = new MenuListViewModel(); menuViewModel.setResource_name(resource_name); menuViewModel.setMenu_name(menu_name); menuNamesList.add(menuViewModel); menuListMap.put(menu_group_name, menuNamesList); } } } catch (Exception ex) { ex.printStackTrace(); }finally { try { closeConnection(connection, rs, ps); } catch (Exception ex) { ex.printStackTrace(); //use logger here } } } printMap(menuListMap); return menuListMap; } </code></pre> <p>But it is not giving me the disired output.</p> <p>My view model:</p> <pre><code>public class MenuListViewModel { private String resource_name = ""; private String menu_name = ""; public String getResource_name() { return resource_name; } public void setResource_name(String resource_name) { this.resource_name = resource_name; } public String getMenu_name() { return menu_name; } public void setMenu_name(String menu_name) { this.menu_name = menu_name; } } </code></pre> <p>and </p> <pre><code> public static void printMap(Map mp) { List menuList = new ArrayList(); MenuListViewModel model; Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); menuList =(List) pairs.getValue(); for(int i=0;i&lt;=menuList.size();i++){ model =(MenuListViewModel) menuList.get(i); System.out.println("Resource_name : "+model.getResource_name()); System.out.println("Menu_name : "+model.getMenu_name()); } System.out.println(pairs.getKey() + " = " + pairs.getValue()); it.remove(); // avoids a ConcurrentModificationException } } </code></pre> <p>Please help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload