I have java.util.LinkedHashMap with Integer as Key and Character as Value. I know the key of the element i want to access. In addition to the element value for the key, i also want to retrieve the next 3 element values so that i can append all the 4 element values and form a string with 4 chars. I am asking for something like how we will do in a java.util.List. Is this feasible by any means in a Map/ordered map? Please suggest any other data structure that can help me achieve this. I am using Java 6.
I have java.util.LinkedHashMap with Integer as Key and Character as Value. I know the
Share
java.util.SortedMap has the methods subMap(fromKey, toKey) and tailMap(fromKey).
You can use the first one if you know the last key you want, but in your case since you want a fixed number of elements, you should try
Note that if the values of the map were not immutable, changes in the list would be reflected in the map and vice versa, since I added the reference of the element to the list(to avoid this you should add a copy of the object in the list).