How to get LinkedHashMap submenu by item index?

I am trying to get a LinkedHashMap swap based on an element index. Am I reinventing the wheel here? It looks like it should already be somewhere in the API:

 public <K,V> LinkedHashMap<K,V> subMap(LinkedHashMap<K,V> map, int fromIndex, int toIndex) { LinkedHashMap<K,V> result = new LinkedHashMap<K,V>(); int i=0; for(Map.Entry<K,V> entry : map.entrySet()) { if(i >= fromIndex && i < toIndex) { result.put(entry.getKey(), entry.getValue()); } i++; } return result; } 

This is the way to go, or there are other better / existing solutions (in the Java API).

+6
source share
1 answer

NavigableMap allows you to get a submap back, but for this you need to specify "from key" and "from" to key ', so you can not do this only by index.

I do not know any other way to do this using the standard API.

+1
source

Source: https://habr.com/ru/post/915713/


All Articles