www.baeldung.com/java-linkedhashmap-first-last-key-value-pair
1 Users
1 Comments
5 Highlights
0 Notes
Top Highlights
a LinkedHashMap maintains the order of elements in the order in which they were inserted.
this order can be either the insertion order or the access order.
linkedHashMap.entrySet().iterator().next() returns the first element in the map:
Entry<String, String> firstEntry = THE_MAP.entrySet().stream().findFirst().get();
Entry<String, String> lastEntry = THE_MAP.entrySet().stream().skip(THE_MAP.size() - 1).findFirst().get();
Toughts & Comments
nash
how to get first or last entry in linkedhashmap? - convert to entry array - StreamAPI - First: stream().findFirst().get() - last: entryset().stream().skip(map.size()-1).findFirst(),get() - Java21, linkedHashMap has head and tail reference.
Glasp is a social web highlighter that people can highlight and organize quotes and thoughts from the web, and access other like-minded people’s learning.