How to fetch first 10 key value pairs in HashMap
Solution 1
How to fetch first 10 key value pairs in
HashMap
HashMap
is unordered. This makes the question ill-posed (unless by "first" you mean "arbitrary").
If you want a consistent ordering of keys, you need to change the type of your map to a SortedMap
, such as TreeMap
.
Alternatively, if it's the oldest elements you're after (i.e. the ones you've inserted first), then LinkedHashMap
is the answer.
As to actually getting the first n
elements, a loop with a counter is a pretty reasonable way to do it.
Solution 2
I am trying to iterate only the first "n" values in my Map, is there any method available or i need to control it only with a count variable.
The closest thing you'll find using only the standard Collections API (which still is slightly worse than a counter variable IMO) is the following:
List<Map.Entry<String, List<String>> entryList =
new ArrayList<Map.Entry<String, List<String>>(map.entrySet());
for (Map.Entry<String, List<String>> entry : entryList.subList(0, 10)) {
List<String> list = entry.getValue();
// Display list of people in City
}
The lengthy type parameters could be avoided either by using the fancy diamond from Java 7:
List<Map.Entry<String, List<String>> entryList = new ArrayList<>(map.entrySet());
or by using iterating over the keys and .get
the corresponding values.
Solution 3
List<List<string>> list = new ArrayList<List<String>>();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
if (list.size() > 9) break;
list.add(entry.getValue());
}
Vijay Selvaraj
Updated on June 17, 2022Comments
-
Vijay Selvaraj almost 2 years
I am trying to iterate only the first "n" values in my Map, is there any method available or i need to control it only with a count variable.
Below is an example, i have sorted a group of names belong to the same city. Now i only want the first 10 city and the person names in it.
for (Map.Entry<String, List<String>> entry : map.entrySet()) { List<String> list = entry.getValue(); // Display list of people in City }
Is there a Map implementation that can hold fixed number of key,value pairs? Please get some directions.
Thanks,
-Vijay Selvaraj
-
default locale about 12 yearsOP stated that he knows how to do this with counter variable
-
Churk about 12 yearsJust looked at both Hashmap and Set, there is no subList method, care to tell us where that came from?
-
NPE about 12 years@Churk: It came from the
ArrayList
that got constructed with the contents ofmap.entrySet()
. -
Churk about 12 years@aix map.entrySet() returns a set, not an arraylist
-
NPE about 12 years@Churk: Right. And the
new ArrayList<...>(...)
creates theArrayList
on whichsubList()
is then called. -
aioobe about 12 years@Churk, I go via the
ArrayList(Collection<? extends E> c)
constructor. Clarified code in the answer slightly. -
Vijay Selvaraj about 12 yearsI tried it out but the compiler complains "The constructor ArrayList<String>(Set<Map.Entry<String,String>>) is undefined"
-
aioobe about 12 yearsSorry.
...
should beMap.Entry<String,String>
. I'll update the answer to be more complete. -
Churk about 12 years@aioobe so there is an implied casting from a set to arraylist. I been away from the java for a few years, and just haven't seen it done this way before.