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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:30:27+00:00 2026-05-23T06:30:27+00:00

I have a List of HashMap ‘s which has key of type Integer and

  • 0

I have a List of HashMap‘s which has key of type Integer and value of type Long.

List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();
        for (int i = 0; i < 10; i++) {
            HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
            mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
            ListofHash.add(mMap);
        }

Now, how do I retrieve the key and value from the list of HashMap?

If using Collection class is the solution, please enlight me.

Update 1:

Actually i am getting the value from the database and putting that into a HashMap

public static List<Map<Integer, Long>> populateMyHashMapFromDB()
{

List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();

for (int i = 0; i < 10; i++) {
                HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
                mMap.put(Integer.valueOf(getIntFromDB(i)), Long.valueOf(getLongFromDB(i)));
                ListofHash.add(mMap);
            }
return ListofHash;


}

where getIntFromDB and getLongFromDB can retreive any int and long values respectively.

Now this is where i want to get my values that i got from DB both keys and values

public void getmyDataBaseValues()
{
List<HashMap<Integer,Long>> ListofHash=populateMyHashMapFromDB();

// This is where i got confused

}

ANSWER This is why Peter Lawrey suggested

public class HashMaps {


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        testPrintHashmapValues(putToHashMap());
        testPrintHashmapValuesWithList(putToHashMapWithList());
    }

    public static Map<Integer, Long> putToHashMap() {
        Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
        for (int i = 0; i < 10; i++) {
            map.put(Integer.valueOf(i), Long.valueOf(200000000000L + i));
        }
        return map;
    }

    public static void testPrintHashmapValues(Map<Integer, Long> map) {
        for (Map.Entry<Integer, Long> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + " value: "
                    + entry.getValue());
        }
    }

    public static List<Map<Integer, Long>> putToHashMapWithList() {
        List<Map<Integer, Long>> listOfHash = new ArrayList<Map<Integer, Long>>();
        for (int i = 0; i < 10; i++) {
            Map<Integer, Long> mMap = new HashMap<Integer, Long>();

            mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
            listOfHash.add(mMap);
        }
        return listOfHash;
    }

    public static void testPrintHashmapValuesWithList(
            List<Map<Integer, Long>> listOfHash) {

        for (int i = 0; i < 10; i++) {
            Map<Integer, Long> map = listOfHash.get(i);
            for (Map.Entry<Integer, Long> entry : map.entrySet()) {
                System.out.println(i + " hashMap");
                System.out.println("key: " + entry.getKey() + " value: "
                        + entry.getValue());
            }
        }
    }

}

My Task is done even without creating a List.

  • 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-05-23T06:30:28+00:00Added an answer on May 23, 2026 at 6:30 am

    Its still not clear to me why you want a list.

    public static Map<Integer, Long> populateMyHashMapFromDB() {
        Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
        for (int i = 0; i < 10; i++) 
                map.put(getIntFromDB(i), getLongFromDB(i));
        return map;
    }
    
    Map<Integer, Long> map = populateMyHashMapFromDB();
    Long value = map.get(key);
    

    This collection isn’t designed to give you the key/values pairs easily. I fyou need to this functionality I would suggest using a different structure.

    Assuming you have a some bad code you cannot change, you can do

    List<Map<Integer, Long>> maps = new ArrayList<Map<Integer, Long>>();
    
    Map<Integer, Long> all = new HashMap<Integer, Long>();
    for(Map<Integer, Long> map: maps)
        all.putAll(map);
    
    for(Map.Entry<Integer, Long> entry: all.entrySet() {
        // do something which each key/value.
    }
    

    In this example you don’t need a List or a Map.

    long[] longs = new long[10];
    for (int i = 0; i < 10; i++) 
        longs[i] = i;
    
    int key = 1;
    int num = longs[key];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of HashMap such as below ArrayList l = new ArrayList
I have a code like this Map<String, List> searchMap = new HashMap<String, List>(); for(int
I have the following Map: Map<String, List<String>> map = new HashMap<String, List<String>>(); which is
I have a HashMap where the key is of type String and the value
I have the following ArrayList ArrayList<HashMap<String, String>> list; HashMap<String, String> map; with the following
I have List I want to sort Desc by Priority, which is int and
I have set up a HashMap like so: Map<String, ArrayList<String>> theAccused = new HashMap<String,
Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>
I have a List and each entry has a dynamic count of attributes which
let say I have this code Map<String, String> list = new HashMap<String, String>(); list.put(number1,

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.