Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8702311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:37:18+00:00 2026-06-13T02:37:18+00:00

I have some values return from database as a result set like following **resource_name

  • 0

I have some values return from database as a result set like following

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

I would like to group resource_name and menu_name based on menu_group_name some thing like the following

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

This is the method i tried,

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;
    }

But it is not giving me the disired output.

My view model:

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;
    }

}

and

 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<=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
        }
}

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:37:19+00:00Added an answer on June 13, 2026 at 2:37 am

    If key does not contain in map then you instantiate new List. Otherwise it will use previous list object if exists.

                       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{
                           menuNamesList = new ArrayList<MenuListViewModel>();
                           menuViewModel = new MenuListViewModel();
                           menuViewModel.setResource_name(resource_name);
                           menuViewModel.setMenu_name(menu_name);
                           menuNamesList.add(menuViewModel);
                           menuListMap.put(menu_group_name, menuNamesList);
                       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some values return from database as a result set like following **resource_name
I have a hashtable . values() method returns values in some order different from
I have some values in a configuration file (XML file) with some values like
I have a method that gets rows from my database. It looks like this:
I use the following stored procedure from my SQL Server 2008 database to return
I have some items in database. Each of'em can have many tags, like Browsable,
I have some values in a dataframe called 'values' ie: Sam Ned Ted Ann
In my C++ application, I have some values that act as codes to represent
I have some default values for testing my code, but for security reasons those
I have some financial values stored as text in a mysql db. the significance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.