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).
serg source share