Reputation: 111
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
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
Reputation: 22673
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:
(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
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
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
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
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
Reputation: 2762
You can use counter. Your foreach loop will exit when your counter reached 100.
Upvotes: -1