Java HashMapの例
HashMap
は、両方のkey=value
をペアとして格納するオブジェクトです。 このHashMap
は、null値とnullキーを許可し、非同期であり、マップの順序を保証しません。
["key","value"] = ["java","example.com"]
1. HashMap Basic
1.1 Add an Item
Map map = new HashMap(); map.put("PostgreSQL", "Free Open Source Enterprise Database");
1.2 Get an Item
map.get("PostgreSQL"); // output : Free Open Source Enterprise Database
1.3 Update an Item
map.put("PostgreSQL", "Still the best!"); map.get("PostgreSQL"); // output : Still the best! // @Since 1.8 map.replace("PostgreSQL", "Still the best! 2"); map.get("PostgreSQL"); // output : Still the best! 2
1.4 Remove an Item
map.remove("PostgreSQL"); map.get("PostgreSQL"); // output : null
1.5 Remove everything
map.clear();
1.6 Get Size
map.size();
2. ループハッシュマップ
HashMap
をループまたは反復する3つの方法があります
2.1 If possible, always uses Java 8 forEach
, simple and nice.
Mapmap = new HashMap<>(); map.forEach((key, value) -> System.out.println("[Key] : " + key + " [Value] : " + value));
2.2 Normal for loop.
Mapmap = new HashMap<>(); for (Map.Entry entry : map.entrySet()) { System.out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue()); }
2.3 Iterator, classic.
Mapmap = new HashMap<>(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); System.out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue()); }
3. Collections.synchronizedMap
3.1 This HashMap
is unsynchronized, if multiple threads access a HashMap
concurrently, it will mess up the values. 複数のスレッドの環境でHashMap
を使用するには、Collections.synchronizedMap(new HashMap<>())
を試して同期マップを作成してください。
HashMapSynchronized.java
package com.example; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class HashMapSynchronized { public static void main(String[] args) { // this map is synchronized Mapmap = Collections.synchronizedMap(new HashMap<>()); map.put("web", 1024); map.put("backend", 2048); map.forEach((key, value) -> System.out.println("[Key] : " + key + " [Value] : " + value)); } }
4. ハッシュマップ
参考のための完全な例。
HashMapExample.java
package com.example; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HashMapExample { public static void main(String[] args) { Mapmap = new HashMap<>(); map.put("PostgreSQL", "Free Open Source Enterprise Database"); map.put("DB2", "Enterprise Database , It's expensive"); map.put("Oracle", "Enterprise Database , It's expensive"); map.put("MySQL", "Free Open SourceDatabase (no more, try MariaDB)"); // Get System.out.println(map.get("PostgreSQL")); // Free Open Source Enterprise Database // Update map.put("PostgreSQL", "Still the best!"); System.out.println(map.get("PostgreSQL")); // Still the best! // @Since 1.8 map.replace("PostgreSQL", "Still the best! 2"); System.out.println(map.get("PostgreSQL")); // Still the best! 2 // Remove map.remove("PostgreSQL"); System.out.println(map.get("PostgreSQL")); // null // Size System.out.println(map.size()); // 3 // loop System.out.println("Iterator loop..."); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); System.out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue()); } System.out.println("for loop..."); for (Map.Entry entry : map.entrySet()) { System.out.println("[Key] : " + entry.getKey() + " [Value] : " + entry.getValue()); } // Java 8 System.out.println("forEach loop..."); map.forEach((key, value) -> System.out.println("[Key] : " + key + " [Value] : " + value)); // clear everything map.clear(); // nothing map.forEach((key, value) -> System.out.println("[Key] : " + key + " [Value] : " + value)); } }
出力
Free Open Source Enterprise Database Still the best! Still the best! 2 null 3 Iterator loop... [Key] : DB2 [Value] : Enterprise Database , It's expensive [Key] : MySQL [Value] : Free Open SourceDatabase (no more, try MariaDB) [Key] : Oracle [Value] : Enterprise Database , It's expensive for loop... [Key] : DB2 [Value] : Enterprise Database , It's expensive [Key] : MySQL [Value] : Free Open SourceDatabase (no more, try MariaDB) [Key] : Oracle [Value] : Enterprise Database , It's expensive forEach loop... [Key] : DB2 [Value] : Enterprise Database , It's expensive [Key] : MySQL [Value] : Free Open SourceDatabase (no more, try MariaDB) [Key] : Oracle [Value] : Enterprise Database , It's expensive