0

我有一些从数据库返回的值作为结果集,如下所示

**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

我想根据 menu_group_name 对 resource_name 和 menu_mane 进行分组,如下所示

 if the menu_group_name is same than group all the corresponding
    resource_name and menu_mane against to menu_group_name .

更新 :

 public Map<String,List> getMenuForLoggedinRole(int roleid){
        Map<String,List> menuListMap = new LinkedHashMap<String,List>();
        List<MenuListViewModel> menuNamesList = new ArrayList<MenuListViewModel>();
        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;
    }

当我打印并看到只有一个值被打印时,其他值没有。请帮助我如何做到这一点。我知道这很简单需要你的帮助。

问候

4

2 回答 2

1

创建一些类,CustomClass,与成员resource_namemenu_name

现在创建一个map<String, List<CustomClass>>

遍历结果集并继续向地图添加数据

menu_group_name将是您的键,您创建的自定义类将是值,在将数据放入地图时,检查该键是否存在,如果是,则检索列表并向其添加新的自定义类,否则使用您的自定义类创建一个新列表并添加放置键值对

于 2012-10-17T10:06:39.073 回答
0

我认为可以做一些这样的代码

class Pair {
                String v1;
                String v2;
                Pair(String value1, String value2){
                    v1=  value1;
                    v2 = value2;
                }
            }
            Map<String , List<Pair>> maps = new HashMap<>();
            List<Pair> keypair = new ArrayList<Pair>();
            keypair.add(new Pair("", ""));
            maps.put("Hello", keypair);
            maps.get("Hello").add(new Pair("", ""));
于 2012-10-17T10:13:15.437 回答