java HashMap 给出了一个无序列表的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7792523/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:28:28  来源:igfitidea点击:

HashMap gives an unordered list of values?

javaandroid

提问by bytebiscuit

I'm having a weird problem with HashMap in Android. I'm putting values into the hashmap which is of the form

我在 Android 中的 HashMap 遇到了一个奇怪的问题。我正在将值放入形式为的哈希图中

HashMap <String,String> sample = new HashMap<String,String>();

However let's say I'm putting the following values in the following order:

但是,假设我按以下顺序放置以下值:

sample.put("ifi1", "video1");
sample.put("ifi2", "video2");
sample.put("ifi3", "video3");
sample.put("ifi4", "video4");
sample.put("ifi5", "video5");
sample.put("ifi6", "video6");
sample.put("ifi7", "video7");
sample.put("ifi8", "video8");
sample.put("ifi9", "video9");

This is just a simple example that is similar to what i have. I only have a bigger list in my actual code. However when I now try to print only the values, I get an unordered list as follows:

这只是一个简单的例子,与我所拥有的相似。我的实际代码中只有一个更大的列表。但是,当我现在尝试仅打印值时,会得到一个无序列表,如下所示:

VIDEOS: video1
VIDEOS: video3
VIDEOS: video2
VIDEOS: video5
VIDEOS: video4
VIDEOS: video7
VIDEOS: video6
VIDEOS: video9
VIDEOS: video8

where in fact I'm expecting it to produces the following list:

事实上,我期望它产生以下列表:

VIDEOS: video1
VIDEOS: video2
VIDEOS: video3
VIDEOS: video4
VIDEOS: video5
VIDEOS: video6
VIDEOS: video7
VIDEOS: video8
VIDEOS: video9

Why is this, any idea?

这是为什么,有什么想法吗?

回答by aioobe

That's right. The HashMapimplementation of Mapdoes not guarantee any order during iteration.

那就对了。的HashMap实现Map不保证迭代期间的任何顺序。

If you want ordering based on insertion...

如果您想根据插入排序...

...have a look at LinkedHashMap:

...看看LinkedHashMap

Map<String, String> sample = new LinkedHashMap<String, String>();

sample.put("ifi1", "video1");
sample.put("ifi2", "video2");
sample.put("ifi3", "video3");
sample.put("ifi4", "video4");
sample.put("ifi5", "video5");
sample.put("ifi6", "video6");
sample.put("ifi7", "video7");
sample.put("ifi8", "video8");
sample.put("ifi9", "video9");

for (String video : sample.values())
    System.out.println(video);

// Prints
video1
video2
video3
video4
video5
video6
video7
video8
video9

If you want ordering based on the keys...

如果您想根据键进行排序...

Use some SortedMapsuch as a TreeMap.

使用一些SortedMap诸如TreeMap.

回答by Sasha Goldshtein

You need to use SortedMapinstead of HashMap. It will guarantee order.

您需要使用SortedMap而不是HashMap. 它将保证秩序。

回答by anujprashar

In HashMap order is not there. Try using LinkedHashMapinstead. LinkedHashMapkeeps track of the keys based on insertion order, so that when you do a call to getKeySet(), you will get the keys back in the order you put them in.

在 HashMap 中是没有顺序的。尝试改用LinkedHashMapLinkedHashMap根据插入顺序跟踪键,这样当您调用 时getKeySet(),您将按照放入的顺序取回键。

回答by Jigar Joshi

Use LinkedHashMapinstead

使用LinkedHashMap替代

回答by Xavi López

You can also use a LinkedHashMapin order to obtain the collection of entries in the same order they were inserted in.

您还可以使用 aLinkedHashMap以按照插入的顺序获取条目的集合。

回答by Miguel Prz

look for a class that implements SortedMap interface. Or for print purposes only, you can order de Set returned from your Map with Collections.sort

寻找一个实现 SortedMap 接口的类。或者仅用于打印目的,您可以使用 Collections.sort 订购从地图返回的 de Set

回答by neeraj t

If you use Simple HashMap then this class use some hashing technique to store the value ,at the time of extracting the data from list its use same hash technique. And Hash function are not ordered. that's why result is coming unordered. In Placed of HashMap you have to use LinkedHashMap to make it ordered List.

如果你使用 Simple HashMap 那么这个类使用一些散列技术来存储值,在从列表中提取数据时,它使用相同的散列技术。并且哈希函数不是有序的。这就是为什么结果是无序的。在 HashMap 中,您必须使用 LinkedHashMap 使其成为有序列表。

回答by mihail

LinkedHashMap<T, T>

keep the insert order

保持插入顺序