Executing a List Order in Java

I was wondering if there is a class that implements Map and List interfaces in Java.

I have a data structure, which is basically a Map . I map strings (IDs) to Image s. But in a certain part of my code, I need to provide the user with all available IDed Images . The only way to do this 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 just write:

 for (Image img : myMap) { // the image is img } 

Does anyone know of such an implementation?

EDIT : after looking at the answers (which everything is correct, voted), now I understand that I will also need to sort the map. When I say β€œsorted,” all I have in mind is that I would like it to have values ​​in a specific order that I could change. I know this is not an original question, but I just realized that I needed it.

EDIT 2 : I seem to be indecisive. I need an ordered map, not a sorted one. Sorry for the confusion, people.

+19
java list map
Jul 31 '09 at 12:11
source share
7 answers

If you need your products in a specific order, LinkedHashMap is your friend - it stores the positions in the order of placement. TreeMap will store your items in the order you define, either by the Comparator you give, or by the compareTo method of the key.

+34
Jul 31 '09 at 12:38
source share

For an ordered map, browse LinkedHashMap . This will keep your keys in insertion order.

If you use SortedMap , it will save the keys in sorted order. ( TreeMap is the most common version.)

What you can use is map.entrySet() . This will allow you to iterate over Set MapEntries.

Learn about javadoc for more information.

+19
Jul 31 '09 at 12:15
source share

You already have a bunch of practical answers. But answering the question directly ...

I wandered if there is a class that implements both Map and List interfaces in Java.

... It is worth mentioning that this is simply impossible. remove(Object) is an obstacle.

In the Map interface, its signature:

 V remove(Object key); 

And in the List interface, this is:

 boolean remove(Object o); 
+7
Jul 31. '09 at 13:43
source share

you can use the Map.values() method, which returns a Collection .

+3
Jul 31 '09 at 12:15
source share

This gives you a set of stored values.

 myMap.values() 
+2
Jul 31 '09 at 12:15
source share

Try the following:

 for (Image img : myMap.values()) { // the image is img } 

For a sorted map, see java.util.SortedMap . java.util.TreeMap is the most used. If you only need a guaranteed iteration order, you can try java.util.LinkedHashMap . It offers iteration in the same order as the elements to display. Or, optionally, in order of last access. If you want to move the key (after adding) to the end of the card, you must explicitly delete it and put it again.

+2
Jul 31 '09 at 12:16
source share

you can use TreeMap , it is sorted according to the natural ordering of its keys or the comparator when creating the map time:

 TreeMap<String, Image> mapByName = new TreeMap<String, Image>(new ByNameComparator()); 

where ByNameComparator () is a comparator. Alternatively, you can use methond () values ​​and sort with Collections.sort ():

 Collection<Image> images = mapByName.values(); Collections.sort(images, new BySizeComparator()); 
0
Jul 31 '09 at 12:34
source share



All Articles