6

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

Vijay Selvaraj
  • 519
  • 2
  • 6
  • 15

3 Answers3

9

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.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Just looked at both Hashmap and Set, there is no subList method, care to tell us where that came from? – Churk Mar 29 '12 at 10:47
  • 1
    @Churk: It came from the `ArrayList` that got constructed with the contents of `map.entrySet()`. – NPE Mar 29 '12 at 10:48
  • @aix map.entrySet() returns a set, not an arraylist – Churk Mar 29 '12 at 10:51
  • @Churk: Right. And the `new ArrayList<...>(...)` creates the `ArrayList` on which `subList()` is then called. – NPE Mar 29 '12 at 10:57
  • @Churk, I go via the [`ArrayList(Collection extends E> c)`](http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#ArrayList%28java.util.Collection%29) constructor. Clarified code in the answer slightly. – aioobe Mar 29 '12 at 11:00
  • I tried it out but the compiler complains "The constructor ArrayList(Set>) is undefined" – Vijay Selvaraj Mar 29 '12 at 11:09
  • Sorry. `...` should be `Map.Entry`. I'll update the answer to be more complete. – aioobe Mar 29 '12 at 11:12
  • @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. – Churk Mar 29 '12 at 11:32
2
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());
}
Churk
  • 4,556
  • 5
  • 22
  • 37