I am trying to get a submap of LinkedHashMap based on element index. Am I reinventing the wheel here? Sounds like this should be somewhere in API already:
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; } Is this the way to go or there are some other better/existing solutions (within Java 6 API).
Mapbe used? Can you use an iterator instead? If so, you could return a custom iterator that returns values (or Map.Entries) between the specified index range. Keep in mind concurrency issues when dealing with index ranges of a collection.