Adnan Ali Ch.
Adnan Ali Ch.

Reputation: 111

How to get a limited number of values from a HashMap or LinkedHashMap?

Say I have a LinkedHashMap containing 216 entries, how would I get the first 100 values (here, of type Object) from a LinkedHashMap<Integer, Object>.

Upvotes: 3

Views: 7176

Answers (7)

M. Justin
M. Justin

Reputation: 21315

The following one-liner will return the first 100 values of a map, or all elements if the map has fewer than 100 values.

List<Object> values = map.values().stream().limit(100).toList();

Upvotes: 0

haylem
haylem

Reputation: 22673

Ugly One-Liner

This ugly one-liner would do (and return a ArrayList<Object> in the question's case):

Collections.list(Collections.enumeration(lhMap.values())).subList(0, 100)

This would work for a HashMap as well, however HashMap being backed by a HashSet there's not guarantee that you will get the first 100 values that were entered; it would work on other types, with similar limitations.

Notes:

  • relatively unefficient (read the Javadoc to know why - though there's worse!),
  • careful when using views (read the Javadoc to know more),
  • I did mention it was ugly.

Step-By-Step Usage Example

(as per the OP's comment)

Map<Integer, Pair<Double, SelectedRoad>> hashmap3 =
  new LinkedHashMap<Integer, Pair<Double, SelectedRoad>>();

// [...] add 216 elements to hasmap3 here somehow

ArrayList<Pair<Double,SelectedRoad>> firstPairs = 
  Collections.list(Collections.enumeration(hashmap3.values())).subList(0, 100)

// you can then view your Pairs' SelectedRow values with them with:
//  (assuming that:
//    - your Pair class comes from Apache Commons Lang 3.0
//    - your SelectedRoad class implements a decent toString() )
for (final Pair<Double, SelectedRoad> p : firstPairs) {
    System.out.println("double: " + p.left);
    System.out.println("road  : " + p.right);
}

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533780

To copy only the records you need with using a library.

Map<Integer, Object> records;

List<Entry<Integer, Object>> firstHundredRecords = new ArrayList<>();
for(Entry<Integer, Object> entry : records.entrySet()) {
    firstHundredRecords.add(entry);
    if (firstHundredRecords.size()>=100) break;
}

Upvotes: 3

Tom Anderson
Tom Anderson

Reputation: 47243

You can do:

Map<Integer, Object> records;
List<Entry<Integer, Object>> firstHundredRecords
    = new ArrayList<Entry<Integer, Object>>(records.entrySet()).subList(0, 100);

Although note that this will copy all the entries from the map.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503080

Well to start with, doing this for HashMap as per your title, doesn't make much sense - HashMap has no particular order, and the order may change between calls. It makes more sense for LinkedHashMap though.

There, I'd use Guava's Iterables.limit method:

Iterable<Object> first100Values = Iterables.limit(map.values(), 100);

or

// Or whatever type you're interested in...
Iterable<Map.Entry<Integer, Object>> firstEntries =
    Iterables.limit(map.entrySet(), 100);

You can then create a list from that, or iterate over it, or whatever you want to do.

Upvotes: 5

dty
dty

Reputation: 18998

Write a loop which uses a Iterator.next() 100 times, and then stops.

I was going to say something about NavigableMap and SortedMap - but their interfaces are defined in terms of keys, not indexes. But they may be useful nevertheless, depending on what your actual underlying problem is.

Upvotes: -2

Rahul Borkar
Rahul Borkar

Reputation: 2762

You can use counter. Your foreach loop will exit when your counter reached 100.

Upvotes: -1

Related Questions