简体   繁体   中英

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

假设我有一个包含216个条目的LinkedHashMap ,我如何从LinkedHashMap<Integer, Object>获取前100个值(此处为Object类型)。

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.

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.

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;
}

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);
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM