在不浪费您更多时间的情况下,以下是Java中ConcurrentHashMap的10个有用示例。通过这些示例,您将学习如何在Jav a中使用ConcurrentHashMap,例如创建地图,插入键值对,更新键值对,删除映射,检查键或值是否存在于地图中,进行迭代。键或值,等等。
1.如何创建具有默认容量的ConcurrentHashMap 首先,让我们学习如何在Java中创建并发哈希图。这是一个使用默认容量创建空的ConcurrentHashMap的示例。
ConcurrentHashMap programmingLanguages = new ConcurrentHashMap(); System.out.println("Empty ConcurrentHashMap : " + programmingLanguages);
2.如何将对象添加到ConcurrentHashMap 创建ConcurrentHashMap之后,就该添加一些映射了。让我们将一些键和值存储到Java中的ConcurrentHashMap中。如果您看下面的代码,与我们之前看到的添加映射的HashMa示例没有什么不同,唯一的区别是它的线程安全。
programmingLanguages.put("Java", Integer.valueOf(18)); programmingLanguages.put("Scala", Integer.valueOf(10)); programmingLanguages.put("C++", Integer.valueOf(31)); programmingLanguages.put("C", Integer.valueOf(41)); System.out .println("ConcurrentHashMap with four mappings : " + programmingLanguages);
3.如何检查键是否存在于ConcurrentHashMap中 现在,您已经添加了映射,现在检查其时间是否是ConcurrentHashMap中是否存在键。这次,我们将使用Map接口中的containsKey()方法,由于CHM实现了Map接口,因此CHM上也可以使用containsKey()方法。
boolean isJavaExist = programmingLanguages.containsKey("Java"); boolean isPythonExist = programmingLanguages.containsKey("Python"); System.out.printf("Does Programming language Map has %s? %b %n", "Java", isJavaExist); System.out.printf("Does Programming language Map contains %s? %b %n", "Python", isPythonExist);
4.如何从Java中的ConcurrentHashMap检索值 这是从Java中的ConcurrentHashMap检索值的示例。这个示例与其他任何映射(例如HashMap或HashTable)非常相似,因为我们使用相同的get()方法从Java中的ConcurrentHashMap检索值。
int howOldIsJava = programmingLanguages.get("Java"); int howOldIsC = programmingLanguages.get("C"); System.out.printf("How old is Java programming langugae? %d years %n", howOldIsJava); System.out.printf("How old is C langugae? %d years %n", howOldIsC);
5.如何检查ConcurrentHashMap中是否存在值 这是检查ConcurrentHashMap中是否存在值的示例。同样,此示例与我们之前看到的HashMap containsValue()示例非常相似。
boolean is41Present = programmingLanguages.containsValue(Integer.valueOf(41)); boolean is31Present = programmingLanguages.containsValue(Integer.valueOf(31)); System.out.printf("Does value 41 is present in ConcurrentHashMap? %b %n", is41Present); System.out.printf("Does value 31 is present in ConcurrentHashMap? %b %n", is31Present);
6.如何在Java中查找ConcurrentHashMap的大小 您可以使用size()方法来查找ConcurrentHashMap中存在多少个键值对。size()方法返回映射总数。
int numberOfMappings = programmingLanguages.size(); System.out.printf("ConcurrentHashMap %s, contains %d mappings %n", programmingLanguages, numberOfMappings);
7.如何在Java中循环遍历ConcurrentHashMap? 有多种方法可以循环遍历Java中的ConcurrentHashMap。实际上,您还可以使用所有四种方式通过ConcurrentHashMap遍历Map。最终,它还实现了java.utill.Map接口,因此它遵守Map的约定
Set> entrySet = programmingLanguages.entrySet(); for (Map.Entry mapping : entrySet) { System.out.printf("Key : %s, Value: %s %n", mapping.getKey(), mapping.getValue()); }
8. PutIfAbsent示例-仅当ConcurrentHashMap中不存在键时才添加键吗? 这是一个有用的方法,仅当地图或字典中尚不存在元素时,才可以使用它来插入元素。这也是常见的ConcurrentHashMap面试问题,经常问初学者。
System.out.printf("Before : %s %n", programmingLanguages); programmingLanguages.putIfAbsent("Java", 22); System.out.printf("After : %s %n", programmingLanguages); programmingLanguages.put("Python", 23); System.out.printf("After : %s %n", programmingLanguages);
9.如何替换ConcurrentHashMap中的映射? 您可以使用replace方法来更新ConcurrentHashMap中的键的值。此方法同时使用键和值,并用新值更新旧值,如下所示:
programmingLanguages.replace("Java", 20); System.out.println("ConcurrentHashMap After replace : " + programmingLanguages);
10.如何从Java中的ConcurrentHashMap中删除键值? 您可以使用ConcurrentHashMap的remove()方法从Map中删除映射。此方法将删除键和值,并且ConcurrentHashMap的大小将减小一,如以下示例所示:
programmingLanguages.remove("C++"); System.out.println("ConcurrentHashMap After remove : " + programmingLanguages)
运行此代码后,将删除“ C ++”键的映射。
11.如何在迭代ConcurrentHashMap时删除键 这是在Java中遍历ConcurrentHashMap时删除键的代码示例。同样,这与从HashMap中删除键没有什么不同,因为我们从Map接口中使用了相同的remove()方法,该方法也由Java中的ConcurrentHashMap类继承。
Iterator keys = programmingLanguages.keySet().iterator(); while (keys.hasNext()) { System.out.printf("Removing key %s from ConcurrentHashMap %n", keys.next()); keys.remove(); }
remove()方法从ConcurrentHashMap中删除当前的Key,就像Iterator对List,Set和Map所做的一样。
12.如何在Java中检查ConcurrentHashMap是否为空? 您可以使用ConcurrentHashMap的isEmpty()方法检查给定的Map是否为空。如果ConcurrentHashMap没有任何映射,则此方法将返回true,如以下示例所示:
boolean isEmpty = programmingLanguages.isEmpty(); System.out.printf("Is ConcurrentHashMap %s is empty? %b ", programmingLanguages, isEmpty);
ConcurrentHashMap示例Java 这是完整的Java程序,您可以在Eclipse中 复制粘贴或从命令行运行它以进行操作:
import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** * Java program to demonstrate how to use Concurrent HashMap in Java by simple examples. * * @author javin */ public class ConcurrentHashMapExamples{ public static void main(String args[]) { // Creates a ConcurrentHashMap with default capacity ConcurrentHashMap programmingLanguages = new ConcurrentHashMap(); System.out.println("Empty ConcurrentHashMap : " + programmingLanguages); // Adding objects into ConcurrentHashMap programmingLanguages.put("Java", Integer.valueOf(18)); programmingLanguages.put("Scala", Integer.valueOf(10)); programmingLanguages.put("C++", Integer.valueOf(31)); programmingLanguages.put("C", Integer.valueOf(41)); System.out.println("ConcurrentHashMap with four mappings : " + programmingLanguages); // Checking if a key exists in ConcurrentHashMap or not boolean isJavaExist = programmingLanguages.containsKey("Java"); boolean isPythonExist = programmingLanguages.containsKey("Python"); System.out.printf("Does Programming language Map has %s? %b %n", "Java", isJavaExist); System.out.printf("Does Programming language Map contains %s? %b %n", "Python", isPythonExist); // Retrieving values from ConcurrentHashMap in Java int howOldIsJava = programmingLanguages.get("Java"); int howOldIsC = programmingLanguages.get("C"); System.out.printf("How old is Java programming langugae? %d years %n", howOldIsJava); System.out.printf("How old is C langugae? %d years %n", howOldIsC); // Checking if a value exists in ConcurrentHashMap boolean is41Present = programmingLanguages.containsValue( Integer.valueOf(41)); boolean is31Present = programmingLanguages.containsValue( Integer.valueOf(31)); System.out.printf("Does value 41 is present in ConcurrentHashMap? %b %n", is41Present); System.out.printf("Does value 31 is present in ConcurrentHashMap? %b %n", is31Present); // Finding Size of ConcurrentHashMap int numberOfMappings = programmingLanguages.size(); System.out.printf("ConcurrentHashMap %s, contains %d mappings %n", programmingLanguages, numberOfMappings); // Loop over ConcurrentHashMap in Java Set> entrySet = programmingLanguages.entrySet(); for (Map.Entry mapping : entrySet) { System.out.printf("Key : %s, Value: %s %n", mapping.getKey(), mapping.getValue()); } //PutIfAbsent Example - Adding keys only if its not present in //ConcurrentHashMap System.out.printf("Before : %s %n", programmingLanguages); programmingLanguages.putIfAbsent("Java", 22); // Already exists System.out.printf("After : %s %n", programmingLanguages); programmingLanguages.put("Python", 23); // Added System.out.printf("After : %s %n", programmingLanguages); // Replacing a Mapping in ConcurrentHashMap programmingLanguages.replace("Java", 20); System.out.println("ConcurrentHashMap After replace : " + programmingLanguages); // Removing key values from ConcurrentHashMap programmingLanguages.remove("C++"); System.out.println("ConcurrentHashMap After remove : " + programmingLanguages); // Removing Keys, while Iterating over ConcurrentHashMap Iterator keys = programmingLanguages.keySet().iterator(); while (keys.hasNext()) { System.out.printf("Removing key %s from ConcurrentHashMap %n", keys.next()); keys.remove(); } // How to check if ConcurrentHashMap is empty boolean isEmpty = programmingLanguages.isEmpty(); System.out.printf("Is ConcurrentHashMap %s is empty? %b ", programmingLanguages, isEmpty); } }
输出:
Empty ConcurrentHashMap : {} ConcurrentHashMap with four mappings : {C=41, Scala=10, Java=18, C++=31} Does Programming language Map has Java? true Does the Programming language Map contain Python? false How old is Java programming language? 18 years How old is C language? 41 years Does value 41 is present in ConcurrentHashMap? true Does value 31 is present in ConcurrentHashMap? true ConcurrentHashMap {C=41, Scala=10, Java=18, C++=31}, contains 4 mappings Key: C, Value: 41 Key: Scala, Value: 10 Key: Java, Value: 18 Key : C++, Value: 31 Before : {C=41, Scala=10, Java=18, C++=31} After : {C=41, Scala=10, Java=18, C++=31} After : {C=41, Python=23, Scala=10, Java=18, C++=31} ConcurrentHashMap After replace : {C=41, Python=23, Scala=10, Java=20, C++=31} ConcurrentHashMap After remove : {C=41, Python=23, Scala=10, Java=20} Removing key C from ConcurrentHashMap Removing key Python from ConcurrentHashMap Removing key Scala from ConcurrentHashMap Removing key Java from ConcurrentHashMap Is ConcurrentHashMap {} is empty? true
这就是Java中的ConcurrentHashMap示例。正如我所说的,在完成这些示例之后,您将对ConcurrentHashMap的工作原理以及如何正确使用它有更好的了解。现在,您对如何在Java中的ConcurrentHashMap上创建,添加,更新,搜索和删除条目有了一个很好的了解。
原文链接:http://codingdict.com