简体   繁体   English

如何获取树形图的最后n个元素

[英]How to get last n elements of a Treemap

I have a TreeMap, that looks like this: 我有一个TreeMap,看起来像这样:

 TreeMap<Instant, HashMap<Type, Double>>

The Instant values are representing hours of a day; Instant值表示一天中的小时数; for each passed hour a value is stored in my map. 每经过一个小时,一个值就会存储在我的地图中。 Now I would like to get the last 24 elements (so the hours of the passed day) of this map. 现在,我想获取这张地图的最后24个元素(即过去一天中的小时数)。 How could I do that? 我该怎么办?

Cheers 干杯

You can use the descendingMap call to get a view on the map which is basically in the reverse order, then take the first 24 entries from that (call iterator etc). 您可以使用descendingMap调用在地图上获取基本上相反的视图,然后从该视图中获取前24个条目(调用iterator等)。 ( Guava 's Iterables provides helpful methods for limiting an iterable etc.) GuavaIterables提供了有用的方法来限制Iterable等。)

EDIT: For example, to get the last 24 elements (in reverse order, and using Guava) you could use: 编辑:例如,要获取最后24个元素(以相反的顺序,并使用番石榴),您可以使用:

List<HashMap<Type, Double>> lastValues = Lists.newArrayList
    (Iterables.limit(map.descendingMap().values(), 24));

您可以将其SortedMap<LocalDate, SortedMap<Hours, Map<Type, Double>>>以便从外部Map获取最新日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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