企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Java 中的`HashMap` > 原文: [https://beginnersbook.com/2013/12/hashmap-in-java-with-example/](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/) `HashMap`是一个基于`Map`的集合类,用于存储键值对,表示为`HashMap<Key, Value>`或`HashMap<K, V>`。此类不保证映射的顺序。它类似于`Hashtable`类,除了它是不同步的并且允许空值(空值和空键)。 它不是有序集合,这意味着它不会按照它们插入`HashMap`的顺序返回键和值。它不会对存储的键和值进行排序。您必须导入`java.util.HashMap`或其超类才能使用`HashMap`类和方法。 ## Java 中的`HashMap`示例: 在这个例子中,我们已经演示了几乎所有`HashMap`类的重要方法。 ```java import java.util.HashMap; import java.util.Map; import java.util.Iterator; import java.util.Set; public class Details { public static void main(String args[]) { /* This is how to declare HashMap */ HashMap<Integer, String> hmap = new HashMap<Integer, String>(); /*Adding elements to HashMap*/ hmap.put(12, "Chaitanya"); hmap.put(2, "Rahul"); hmap.put(7, "Singh"); hmap.put(49, "Ajeet"); hmap.put(3, "Anuj"); /* Display content using Iterator*/ Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mentry = (Map.Entry)iterator.next(); System.out.print("key is: "+ mentry.getKey() + " & Value is: "); System.out.println(mentry.getValue()); } /* Get values based on key*/ String var= hmap.get(2); System.out.println("Value at index 2 is: "+var); /* Remove values based on key*/ hmap.remove(3); System.out.println("Map key and values after removal:"); Set set2 = hmap.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry mentry2 = (Map.Entry)iterator2.next(); System.out.print("Key is: "+mentry2.getKey() + " & Value is: "); System.out.println(mentry2.getValue()); } } } ``` 输出: ```java key is: 49 & Value is: Ajeet key is: 2 & Value is: Rahul key is: 3 & Value is: Anuj key is: 7 & Value is: Singh key is: 12 & Value is: Chaitanya Value at index 2 is: Rahul Map key and values after removal: Key is: 49 & Value is: Ajeet Key is: 2 & Value is: Rahul Key is: 7 & Value is: Singh Key is: 12 & Value is: Chaitanya ``` ## `HashMap`类方法 以下是`HashMap`类中可用的方法列表。我还在本文末尾介绍了使用这些方法的示例。 1. `void clear()`:它从指定的`Map`中删除所有键和值对。 2. `Object clone()`:它返回映射所有映射的副本,用于将它们克隆到另一个映射中。 3. `boolean containsKey(Object key)`:它是一个布尔函数,它根据是否在映射中找到指定的键返回`true`或`false`。 4. `boolean containsValue(Object Value)`:与`containsKey()`方法类似,但它查找指定的值而不是`key`。 5. `Value get(Object key)`:返回指定键的值。 6. `boolean isEmpty()`:它检查映射是否为空。如果映射中没有键值映射,则此函数返回`true`,否则返回`false`。 7. `Set keySet()`:返回从映射中获取的键的`Set`。 8. `Value put(Key k, Value v)`:将键值映射插入到映射中。用于上面的例子。 9. `int size()`:返回映射的大小 - 键值映射的数量。 10. `Set values()`:返回映射的值集合。 11. `Value remove(Object key)`:删除指定键的键值对。用于上面的例子。 12. `void putAll(Map m)`:将映射的所有元素复制到另一个指定的映射。 ## `HashMap`教程 以下是在`HashMap`类上发布的教程列表。学习快乐! #### `HashMap`基础知识 1. [如何迭代`HashMap`](https://beginnersbook.com/2013/12/how-to-loop-hashmap-in-java/) 2. [按键和值排序`HashMap`](https://beginnersbook.com/2013/12/how-to-sort-hashmap-in-java-by-keys-and-values/) 3. [获取`HashMap`的大小](https://beginnersbook.com/2014/07/java-get-size-of-hashmap-example/) 4. [从`HashMap`中删除键值映射](https://beginnersbook.com/2014/07/java-remove-mapping-from-hashmap-example/) 5. [从`HashMap`中删除所有映射](https://beginnersbook.com/2014/07/java-remove-all-mappings-from-hashmap-example/) 6. [如何检查`HashMap`是否为空?](https://beginnersbook.com/2014/08/how-to-check-if-a-hashmap-is-empty-or-not/) #### 获取/搜索 1. [检查`HashMap`中是否存在特定键](https://beginnersbook.com/2014/07/java-check-if-a-particular-key-exists-in-hashmap-example/) 2. [检查`HashMap`中是否存在特定值](https://beginnersbook.com/2014/07/java-check-if-a-particular-value-exists-in-hashmap-example/) #### 序列化/同步 1. [序列化`HashMap`](https://beginnersbook.com/2013/12/how-to-serialize-hashmap-in-java/) 2. [同步`HashMap`](https://beginnersbook.com/2013/12/how-to-synchronize-hashmap-in-java-with-example/) #### 差异 1. [`HashMap` 与 `ArrayList`](https://beginnersbook.com/2013/12/difference-between-arraylist-and-hashmap-in-java/) 2. [`HashMap` vs `Hashtable`](https://beginnersbook.com/2014/06/difference-between-hashmap-and-hashtable/) 3. [`HashSet` vs `HashMap`](https://beginnersbook.com/2014/08/hashset-vs-hashmap-java/) #### 其他教程 1. [`HashMap Iterator`示例](https://beginnersbook.com/2014/07/java-hashmap-iterator-example/) 2. [将一个`HashMap`复制到另一个](https://beginnersbook.com/2014/08/how-to-copy-one-hashmap-content-to-another-hashmap/) 3. [使用键从`HashMap`获取值](https://beginnersbook.com/2014/08/hashmap-get-value-from-key-example/) 4. [从`HashMap`获取键集视图](https://beginnersbook.com/2014/08/java-get-set-view-of-keys-from-hashmap/) 5. [克隆`HashMap`](https://beginnersbook.com/2014/08/clone-a-hashmap-in-java/) #### 参考: [`HashMap`文档](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)