Java中的有序列表映射实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1212198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ordered List Map implementation in Java
提问by Savvas Dalkitsis
I was wondering if there is a class out there that implements both the Map
and List
interfaces in Java.
我想知道是否有一个类在 Java 中同时实现Map
和List
接口。
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:
我有一个主要是Map
. 我将字符串(ID)映射到Image
s。但是在我代码的特定部分,我需要向用户展示所有可用的 IDed Images
。到目前为止,唯一的方法是这样写:
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:
所以如果有一个同时实现这两个类的类会很好Map
,List
所以我可以简单地写:
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.
编辑 2:看来我优柔寡断。我需要的是有序地图,而不是排序地图。对不起,人们的困惑。
采纳答案by Tadeusz Kopec
If you need your items in a specific order, LinkedHashMapis 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.
如果您需要按特定顺序排列的项目,LinkedHashMap是您的朋友——它可以按插入顺序保存项目。TreeMap 将按照您提供的 Comparator 或键的 compareTo 方法定义的顺序保存您的项目。
回答by jjnguy
For an ordered Map, look at the LinkedHashMap
. That will keep your keys in the order of insertion.
对于有序的 Map,请查看LinkedHashMap
. 这将使您的密钥按插入顺序排列。
If you use a SortedMap
it will keep the keys in sorted order. (The TreeMap
is the most common implementation.)
如果您使用 a SortedMap
,它将按排序顺序保存键。(这TreeMap
是最常见的实现。)
What you can use is map.entrySet()
. That will allow you to iterate over the Set of MapEntries.
您可以使用的是map.entrySet()
. 这将允许您迭代 MapEntries 集。
Check out the javadocfor a bit more info.
查看javadoc了解更多信息。
回答by Stroboskop
This gives you a collection of the stored values
这为您提供了存储值的集合
myMap.values()
回答by akf
you can use the Map.values()
method, which returns a Collection
.
您可以使用该Map.values()
方法,该方法返回一个Collection
.
回答by Rorick
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.
对于排序地图,请查看java.util.SortedMap
实现。java.util.TreeMap
是最常用的选择。如果您只需要保证迭代顺序,您可以尝试java.util.LinkedHashMap
. 它以与将元素放入映射相同的顺序提供迭代。或者,可选地,按照最后访问的顺序。如果您想将键(一旦添加)移动到地图的末尾,您必须明确删除它并重新放置。
回答by dfa
you can use a TreeMapit is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time:
您可以使用TreeMap它根据其键的自然顺序进行排序,或者通过在地图创建时提供的 Comparator 进行排序:
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():
其中 ByNameComparator() 是一个比较器。或者,您可以使用 values() 方法并使用 Collections.sort() 进行排序:
Collection<Image> images = mapByName.values();
Collections.sort(images, new BySizeComparator());
回答by Grzegorz Oledzki
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.
我在徘徊是否有一个类在 Java 中实现了 Map 和 List 接口。
... it's worth to mention that it's simply impossible. remove(Object)
method is the obstacle.
…… 值得一提的是,这简直是不可能的。remove(Object)
方法是障碍。
In Map
interface its signature is:
在Map
接口中,它的签名是:
V remove(Object key);
And in List
interface it's:
在List
界面中它是:
boolean remove(Object o);