1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HashMap#valuesの並び順がjdk1.8と7では異なる

Posted at

HashMap#valuesの並び順がjdk1.7と1.8で異なってて軽くデグレってたのでメモ。
理由とか深くは追ってません。
そもそもHashMap#valuesに特定の並び順を期待するのは良く無いことですw

実行環境

MacOS X 10.9.5
JDK
1.6.0_65(Macのビルトイン)
1.7.0_71
1.8.0_25

MapOrder.java
import java.util.HashMap;
import java.util.Map;

public class MapOrder {
	public static void main(String[] args) {
		putAndPrintMap(new HashMap<String, Integer>());
	}

	public static void putAndPrintMap(Map<String, Integer> map) {
		for (int i = 0; i < 5; i++) {
			map.put(Integer.toString(i), i);
		}
		for (Integer value : map.values()) {
			System.out.println(value);
		}
	}
}

結果
// jdk1.6.0_65
3
2
1
0
4
// jdk1.7.0_71
3
2
1
0
4
// jdk1.8.0_25
0
1
2
3
4
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?