I was wondering if there is a class out there that implements both the Map
and List
interfaces in Java.
I have a data structure that is primarily a Map
. I map strings (IDs) to Image
s. But in a specific part of my code, I need to present the user with all the available IDed Images
. The only way to do that so far is to write this:
for (String id : myMap.keySet()) {
// get the image like this "myMap.get(id)"
}
So it would be nice to have a class that implements both Map
and List
so I could simply write:
for (Image img : myMap) {
// the image is img
}
Does anyone know of such an implementation?
EDIT: After viewing the answers (which are all correct, voted up), I now realize I would also need the map to be sorted. When I say "sorted", all I mean is that I would like it to have the values in a specific order, one that I would be able to modify. I know this is not the original question, but I just realized that I need that.
EDIT 2: It seems I am indecisive. What I need is an ordered map, not a sorted one. Sorry for the confusion, people.
7 Answers 7
If you need your items in a specific order, LinkedHashMap is your friend - it keeps items in insertion order. TreeMap will keep your items in an order defined by either a Comparator you give or a compareTo method of the key.
-
it would be nice to be able to change the item order though :(Savvas Dalkitsis– Savvas Dalkitsis07/31/2009 12:45:33Commented Jul 31, 2009 at 12:45
-
I think, you can re-put item to the end of map by removing it and putting again.Rorick– Rorick07/31/2009 12:47:38Commented Jul 31, 2009 at 12:47
For an ordered Map, look at the LinkedHashMap
. That will keep your keys in the order of insertion.
If you use a SortedMap
it will keep the keys in sorted order. (The TreeMap
is the most common implementation.)
What you can use is map.entrySet()
. That will allow you to iterate over the Set of MapEntries.
Check out the javadoc for a bit more info.
-
thanks for the answer. please check out my edits as my question has slightly changed.Savvas Dalkitsis– Savvas Dalkitsis07/31/2009 12:30:32Commented Jul 31, 2009 at 12:30
-
Thanks. I usually prefer brevity over long-windedness.jjnguy– jjnguy07/31/2009 12:31:03Commented Jul 31, 2009 at 12:31
You've got already a bunch of practical answers. But answering directly the question...
I was wandering if there is a class out there that implements both Map and List interfaces in Java.
... it's worth to mention that it's simply impossible. remove(Object)
method is the obstacle.
In Map
interface its signature is:
V remove(Object key);
And in List
interface it's:
boolean remove(Object o);
you can use the Map.values()
method, which returns a Collection
.
-
nice answer. please check out my edits as my question has slightly changed.Savvas Dalkitsis– Savvas Dalkitsis07/31/2009 12:27:04Commented Jul 31, 2009 at 12:27
This gives you a collection of the stored values
myMap.values()
-
nice answer. please check out my edits as my question has slightly changed.Savvas Dalkitsis– Savvas Dalkitsis07/31/2009 12:27:49Commented Jul 31, 2009 at 12:27
-
A HashMap will produce a value collection sorted by hash - which is usually not what you want. So go with jinguy's answer and use a TreeMap and then call values().Stroboskop– Stroboskop07/31/2009 12:42:22Commented Jul 31, 2009 at 12:42
Try this:
for (Image img : myMap.values()) {
// the image is img
}
For sorted map look at java.util.SortedMap
implementations. java.util.TreeMap
is the most often choice. If you need just guaranteed iteration order you can try java.util.LinkedHashMap
. It offers iteration in the same order as you put elements to map. Or, optionally, in last-accessed order. If you'd like to move key (once added) to the end of map, you must explicitly remove it and put again.
-
nice answer. please check out my edits as my question has slightly changed.Savvas Dalkitsis– Savvas Dalkitsis07/31/2009 12:26:24Commented Jul 31, 2009 at 12:26
you can use a TreeMap it is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time:
TreeMap<String, Image> mapByName = new TreeMap<String, Image>(new ByNameComparator());
where ByNameComparator() is a Comparator. Alternatively you can use the values() methond and sort using Collections.sort():
Collection<Image> images = mapByName.values();
Collections.sort(images, new BySizeComparator());