目录html
HashMap自1.2起就是java集合的一部分,它提供了java基本的map映射接口实现。经过将数据储存在(Key, Value)也就是键值对中。想要获得值就必须知道对应的键。之因此用 HashMap 命名是由于它使用了一个叫作 Hashing 的技术,Hashing 是一种能够将长字符串转换成短字符串而且表示的字符串相同。段字符串的好处是更快的标记和索引。HashSet一样偶尔使用HashMap,偶尔也会使用链表来储存键值对。java
几个HashMap的重要属性:api
HashMap的内在包含了一个节点数组,其中一个节点能够表示成一个包含四个领域的类。数组
Node包含了它自身对象的引用,因此这是一个链表。
HashMap:
多线程
容量即容器的承载量,一旦HashMap被实例化即有了初始容量。负荷系数使用来测量什么时候从新散列须要中止。从新散列是用来提高容量的步骤。在HashMap中容量是乘以2。负荷系数一样是测量那些部分在从新散列以前容许被填入。当HashMap中的条目数增长了当前容量和负载因子的乘积时,就会增长容量,这时就进行了从新散列。若是初始容量持续增高那么从新散列用不会中止,可是经过不断的增高初始容量它增长了迭代的时间复杂度,因此他应该谨慎选择来提高他的性能,当设置初始容量时应该将预计的values考虑在内。一般状况下负荷系数设置为0.75,它较好地平衡了时间和空间花费。负荷系数的value在0-1之间oracle
HashMap是非同步的也就是说多线程能够同时访问。若是多线程同是访问这个类并且至少有一个线程改变他的结构那么就有必要让他在外部同步。它是经过同步一些封装了映射的对象来完成的。若是不存在这样的对象,则能够将其封装在Collections.synchronizedMap()中,以使HashMap同步,并避免意外的非同步访问。
示例以下:app
Map m = Collections.synchronizedMap(new HashMap(...));Map
如今Map m是同步的了函数
若是在建立迭代器以后进行任何结构修改(除了经过迭代器的remove方法以外的任何方式),该类的迭代器都是快速失效的。在迭代器失败时,它将抛出ConcurrentModificationException。性能
HashMap提供了4个构造函数,每一个的访问修饰符都是公共的:this
Example:
// Java program to illustrate // Java.util.HashMap import java.util.HashMap; import java.util.Map; public class GFG { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<>(); print(map); map.put("vishal", 10); map.put("sachin", 30); map.put("vaibhav", 20); System.out.println("Size of map is:-"+ map.size()); print(map); if (map.containsKey("vishal")) { Integer a = map.get("vishal"); System.out.println("value for key"+ " \"vishal\" is:- "+ a); } map.clear(); print(map); } public static void print(Map<String, Integer> map) { if (map.isEmpty()) { System.out.println("map is empty"); } else { System.out.println(map); } } }
输出:
map is empty Size of map is:- 3 {vaibhav=20, vishal=10, sachin=30} value for key "vishal" is:- 10 map is empty
HashMap为基本操做提供了恒定的时间复杂度,若是正确编写了散列函数,而且正确地将元素分散到各个buckets中,则使用get和put。遍历全部的HashMap取决于HashMap的容量和许多键-值对。一般来讲,它与容量+大小成正比。容量是HashMap中的buckets。因此一开始在HashMap中保留大量bucket不友好。
//将字符串映射成为整数键 // Java code to illustrate the clear() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Clearing the hash map using clear() hash_map.clear(); // Displaying the final HashMap System.out.println("Finally the maps look like this: " + hash_map); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} Finally the maps look like this: {}
//将整数映射成为字符串 // Java code to illustrate the clear() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Clearing the hash map using clear() hash_map.clear(); // Displaying the final HashMap System.out.println("Finally the maps look like this: " + hash_map); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} Finally the maps look like this: {}
//将字符串映射为整数 // Java code to illustrate the containsKey() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Checking for the key_element '20' System.out.println("Is the key '20' present? " + hash_map.containsKey(20)); // Checking for the key_element '5' System.out.println("Is the key '5' present? " + hash_map.containsKey(5)); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} Is the key '20' present? true Is the key '5' present? false
//将整数映射成为字符串 // Java code to illustrate the containsKey() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Checking for the key_element 'Welcomes' System.out.println("Is the key 'Welcomes' present? " + hash_map.containsKey("Welcomes")); // Checking for the key_element 'World' System.out.println("Is the key 'World' present? " + hash_map.containsKey("World")); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} Is the key 'Welcomes' present? true Is the key 'World' present? false
// 将字符串映射为整数 // Java code to illustrate the containsValue() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Checking for the Value 'pomelos' System.out.println("Is the value 'pomelos' present? " + hash_map.containsValue("pomelos")); // Checking for the Value 'World' System.out.println("Is the value 'World' present? " + hash_map.containsValue("World")); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} Is the value 'pomelos' present? true Is the value 'World' present? false
// 经整数映射为字符串 // Java code to illustrate the containsValue() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Checking for the Value '10' System.out.println("Is the value '10' present? " + hash_map.containsValue(10)); // Checking for the Value '30' System.out.println("Is the value '30' present? " + hash_map.containsValue(30)); // Checking for the Value '40' System.out.println("Is the value '40' present? " + hash_map.containsValue(40)); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} Is the value '10' present? false Is the value '30' present? true Is the value '40' present? false
// 将字符串映射为数字 // Java code to illustrate the clone() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Displaying the cloned HashMap using clone() System.out.println("The cloned map look like this: " + hash_map.clone()); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} The cloned map look like this: {25=Welcomes, 10=pomelos, 20=pomelos, 30=You, 15=4}
// 将整数映射为字符串 // Java code to illustrate the clone() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Displaying the cloned HashMap using clone() System.out.println("The cloned map look like this: " + hash_map.clone()); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} The cloned map look like this: {pomelos=20, 4=15, You=30, Welcomes=25}
// 将整数映射成为字符串 // Java code to illustrate the isEmpty() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("The Mappings are: " + hash_map); // Checking for the emptiness of Map System.out.println("Is the map empty? " + hash_map.isEmpty()); } }
输出:
The Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} Is the map empty? false
// 对于空hashMap // Java code to illustrate the isEmpty() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Displaying the HashMap System.out.println("The Mappings are: " + hash_map); // Checking for the emptiness of Map System.out.println("Is the map empty? " + hash_map.isEmpty()); } }
输出:
The Mappings are: {} Is the map empty? true
// 字符串映射成整数 // Java code to illustrate the entrySet() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using entrySet() to get the set view System.out.println("The set is: " + hash_map.entrySet()); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} The set is: [20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4]
// 讲整数映射成为字符串 // Java code to illustrate the entrySet() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using entrySet() to get the set view System.out.println("The set is: " + hash_map.entrySet()); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} The set is: [4=15, pomelos=20, You=30, Welcomes=25]
// 将字符串映射为整数值 // Java code to illustrate the keySet() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using keySet() to get the set view of keys System.out.println("The set is: " + hash_map.keySet()); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} The set is: [20, 25, 10, 30, 15]
// 将整数映射成为字符串 // Java code to illustrate the keySet() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using keySet() to get the set view of keys System.out.println("The set is: " + hash_map.keySet()); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} The set is: [4, pomelos, You, Welcomes]
* 语法 hash_map.keySet() * 参数: 无参数 * 返回值:该方法返回一个具备散列映射键的集合。 * 示例以下:
// 将字符串映射为整数 // Java code to illustrate the keySet() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using keySet() to get the set view of keys System.out.println("The set is: " + hash_map.keySet()); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} The set is: [20, 25, 10, 30, 15]
//将整数映射为字符串 // Java code to illustrate the keySet() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using keySet() to get the set view of keys System.out.println("The set is: " + hash_map.keySet()); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} The set is: [4, pomelos, You, Welcomes]
* 语法: Hash_Map.size() * 参数: 无需参数 * 返回值: 该方法返回映射的大小,这也表示映射中存在的键值对的数量。 * 示例以下
//将字符串映射成为整数 // Java code to illustrate the size() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Displaying the size of the map System.out.println("The size of the map is " + hash_map.size()); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} The size of the map is 5
// 将整数映射成为字符串 // Java code to illustrate the size() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Displaying the size of the map System.out.println("The size of the map is " + hash_map.size()); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} The size of the map is 4
// 当传递一个存在key // Java code to illustrate the put() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Inserting existing key along with new value String returned_value = (String)hash_map.put(20, "All"); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displayin the new map System.out.println("New map is: " + hash_map); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} Returned value is: pomelos New map is: {20=All, 25=Welcomes, 10=pomelos, 30=You, 15=4}
// 当传递一个新值 // Java code to illustrate the put() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Inserting existing key along with new value String returned_value = (String)hash_map.put(50, "All"); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displayin the new map System.out.println("New map is: " + hash_map); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} Returned value is: null New map is: {50=All, 20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
// 将字符串映射为整数 // Java code to illustrate the putAll() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Creating a new hash map and copying HashMap<Integer, String> new_hash_map = new HashMap<Integer, String>(); new_hash_map.putAll(hash_map); // Displaying the final HashMap System.out.println("The new map looks like this: " + new_hash_map); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} The new map looks like this: {25=Welcomes, 10=pomelos, 20=pomelos, 30=You, 15=4}
// 将整数映射成为字符串 // Java code to illustrate the putAll() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Creating a new hash map and copying HashMap<String, Integer> new_hash_map = new HashMap<String, Integer>(); new_hash_map.putAll(hash_map); // Displaying the final HashMap System.out.println("The new map looks like this: " + new_hash_map); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} The new map looks like this: {pomelos=20, 4=15, You=30, Welcomes=25}
// 当传递一个已存在key // Java code to illustrate the remove() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Removing the existing key mapping String returned_value = (String)hash_map.remove(20); // Verifying the returned value System.out.println("Returned value is: "+ returned_value); // Displayin the new map System.out.println("New map is: "+ hash_map); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} Returned value is: pomelos New map is: {25=Welcomes, 10=pomelos, 30=You, 15=4}
// 当传递一个新key // Java code to illustrate the remove() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Removing the new key mapping String returned_value = (String)hash_map.remove(50); // Verifying the returned value System.out.println("Returned value is: "+ returned_value); // Displayin the new map System.out.println("New map is: "+ hash_map); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 3`0=You, 15=4} Returned value is: null New map is: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
// 将字符串映射为整数 // Java code to illustrate the values() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); // Mapping string values to int keys hash_map.put(10, "pomelos"); hash_map.put(15, "4"); hash_map.put(20, "pomelos"); hash_map.put(25, "Welcomes"); hash_map.put(30, "You"); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using values() to get the set view of values System.out.println("The collection is: " + hash_map.values()); } }
输出:
Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4} The collection is: [pomelos, Welcomes, pomelos, You, 4]
// 将整数映射成字符串 // Java code to illustrate the values() method import java.util.*; public class Hash_Map_Demo { public static void main(String[] args) { // Creating an empty HashMap HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); // Mapping int values to string keys hash_map.put("pomelos", 10); hash_map.put("4", 15); hash_map.put("pomelos", 20); hash_map.put("Welcomes", 25); hash_map.put("You", 30); // Displaying the HashMap System.out.println("Initial Mappings are: " + hash_map); // Using values() to get the set view of values System.out.println("The collection is: " + hash_map.values()); } }
输出:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} The collection is: [15, 20, 30, 25]
https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
https://www.geeksforgeeks.org/hashset-in-java/