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 8600537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:38:22+00:00 2026-06-12T01:38:22+00:00

I am retrieving number of values from database, that need to be added to

  • 0

I am retrieving number of values from database, that need to be added to a list and then to a MAP with specific Key based on their values, for example,

row 1   name = "A" category = "1"
row 2   name = "B" category = "2"
row 3   name = "C" category = "1"
row 4   name = "D" category = "3"

I need to put row1 and row 3 in a list and each of row2 and row 4 in a separate list, then
put these three lists into MAP and use the category names (1,2,3) as keys of MAP.

MAP
   1 LIST1 (values of 2 rows)
   2 LIST2 (values of 1 row)
   3 LIST3 (values of 1 row)

I am not sure how to add them to the list and MAP.
My constraint is that not everytime that I retrieve the values the same rows will be retrieved, so everytime I may need different number of lists to keep the values of each category that has been retrieved.

My code is as following,

 ResultSet rs = st.executeQuery(SelectRows);
 Map map = new HashMap();
 if(rs.next())
 {
    item.setRowID(rs.getString("ID"));
    item.setName(rs.getString("Name"));                       
    item.setValue(rs.getString("Value"));
    item.setCat(rs.getString("Cat"));
    if(!map.containsKey(rs.getString("Cat"))){
        map.put(rs.getString("CatID"), new ArrayList());
    }
    map.get(rs.getString("CatID")).add(row);
 }

the last line runs into this error > “” the method add(Rows) is undefined for the type Object.
even if I add the object to a list I run into the same error, the method add(List) is undefined for the type Object.

  • 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-12T01:38:23+00:00Added an answer on June 12, 2026 at 1:38 am

    You need to create a map of lists. As you read the records, if there exist a key (here is category) then add the current row to the list

     Map map = new HashMap()
     for( Row row : resultset ) {
          if(!map.containsKey(row.category)){
                map.put(row.category, new ArrayList());
          }
          map.get(row.category).add(row.name);
      }
    

    UPDATE

    the problem is that you need to specify generic type here

     Map<String, List<Rows>> map = new HashMap<String, List<Rows>>()
     while( rs.next() ) {
          Rows row = new Rows();
          /*
           * code to initialize the values of row from the record
           */
          String category = rs.getString("Cat");
          if(!map.containsKey(category)){
                map.put(category, new ArrayList<Rows>());
          }
          map.get(category).add(row);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am retrieving an array values from database which save multi dimensional array..i want
I'm retrieving a signed int from a SQL Server database and need to convert
I am retrieving two date time values from the database. Once the value is
We need to store a list of data we pull from another table that
I am retrieving a very large number of records with multiple result sets from
I'm retrieving a set of location names from a MySQL database using PHP and
I am retrieving 3 fields from a database, and by default, only the username
I am maintaining a Python script that uses xlrd to retrieve values from Excel
I'm trying to load a specific key from a dictionary, with keys like character
I have an activity that displays a number. The number is obtained from SharedPreferences

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.