| by Arround The Web | No comments

How to Use Bulk ConcurrentHashMap Operations in Java?

The “ConcurrentHashMap” offers bulk operations that can be easily inserted inside the Java file and consumes less time and effort for the user. These operations are specifically used for iterating, removing, and searching elements that reside in the “ConcurrentHashMap”. Several different variations come with each operation, which provides freedom to the user to use these operations differently.

This article demonstrates the procedure and practical implementation of the ConcurrentHashMap Bulk operations in Java.

How to Use Bulk ConcurrentHashMap Operations in Java?

The Bulk operation provides fast and easy-to-implement and understandable code to handle elements of the ConcurrentHashMap. Follow the below guide to understand each Bulk operation in detail:

Method 1: Using forEach() Method

To iterate over each entity that is available inside the “ConcurrentHashMap”, the “forEach()” method can be used. This method can traverse any part of the ConcurrentHashMap and apply specified functions on the targeted elements.

This method takes parameters to start the iteration:

Let us break down these parameters:

  • The “parallelismThreshold” is the first parameter, it specifies the iteration number after which the parallel execution can happen.
  • The second parameter is the “transformer” which can change the result to display only the specified part as an output.

For a better understanding visit the below code:

import java.util.concurrent.ConcurrentHashMap;
class Main {
 public static void main(String[] args) //creation of main() method

 { // ConcurrentHashMap Declaration

  ConcurrentHashMap<String, Integer> Team= new ConcurrentHashMap<>();
  Team.put("Thor", 2);
  Team.put("Strange", 4);
  Team.put("Hawkeye", 6);
  System.out.println("ConcurrentHashMap: " + Team);
  Team.forEach(5, (k, v) -> System.out.println("key: " + k + " value: " + v));
  System.out.print("Values are ");
  Team.forEach(5, (k, v) -> k, (k) -> System.out.print(k + ", "));
 }
}

Explanation of the above code block:

  • First, the “ConcurrentHashMap” is created with three dummy elements and displays this map on the console.
  • Next, the “forEach()” method is applied to the Map. The first parameter is provided with a value of “5” to execute parallel execution after “5” iterations.
  • Now, the second parameter returns the “key/value” mapping in a default format. Where “k” represents the key and “v” represents value.
  • After that, in the same manner, if the programmer wants to display only “keys”. Then simply points to the second parameter to retrieve only “keys”. And prints the results on the console.

After the end of the execution phase:

The above output shows that the iterator retrieves the element of the “concurrentHashMap” using the “foreach()” method.

Additionally, there are three variations of the forEach() Method:

  • The “forEachEntry()” method performs the specified function for both keys and corresponding values.
  • The “forEachKey()” method applies functions only on keys stored in Map.
  • The “forEachValue()” method is used to apply specified operations only on the values.

Method 2: Using search() Method

The “search()” method is used to find, retrieve, or search the specific element of the “concurrentHashMap”. The arrow function is inserted inside this method as a second parameter that helps in searching the specified element. It also accepts the “parallelismThreshold” as an optional first parameter to execute the operation in parallel.

Visit the below code for practical implementation of the “search()” method:

import java.util.concurrent.ConcurrentHashMap;
class Main {
 public static void main(String[] args) //creation of main() method

 { // ConcurrentHashMap Declaration

  ConcurrentHashMap<String, Integer> Team= new ConcurrentHashMap<>();
  Team.put("Thor", 2);
  Team.put("Strange", 4);
  Team.put("Hawkeye", 6);
  Team.put("Panther", 8);
  System.out.println("ConcurrentHashMap: " + Team);
  String key = Team.search(5, (k, v) -> {return v == 4 ? k: null;});
  System.out.println("Searched value: " + key);
 }
}

Description of the above code:

  • First, elements are inserted in the “ConcurrentHashMap” in the same manner and displayed on the console.
  • Next, the “search()” method is utilized which has a value of “5” as the first parameter which is considered the “parallelismThreshold”.
  • After that, the “{return v == 4 ? k: null;}” is passed as a value of the second parameter function. This iterates each element of the “ConcurrentHashMap” till the element that has a “value” of “4” does not reach. If this element is not found then, display the element that has a “key” of null.

After the end of the execution phase:

The above snapshot shows the element that has a key of “4” is retrieved using the “search()” method.

There are also variations of the search() method, described below:

  • In the “searchEntries()” method, the element in the key/value format is retrieved.
  • In the “searchKeys()” method, the search function applies specified operations only on keys.
  • In the “searchValues()” method, the search function applies specified operations only on the values.

Method 3: Using reduce() Method

The “reduce()” method selects all elements of the “ConcurrentHashMap” at once and applies some operation or a common task on all the elements at a time. The task can be some arithmetic operations, logical operations, etc. The “reducer” expression is added next to the “transformer” part inside the “reduce()” method parenthesis.

Visit the below code for a better explanation:

import java.util.concurrent.ConcurrentHashMap;
class Main {
 public static void main(String[] args) //creation of main() method

 { // ConcurrentHashMap Declaration

  ConcurrentHashMap<String, Integer> Team= new ConcurrentHashMap<>();
  Team.put("Thor", 2);
  Team.put("Strange", 4);
  Team.put("Hawkeye", 6);
  Team.put("Panther", 8);
  System.out.println("ConcurrentHashMap: " + Team);
  int addition= Team.reduce(5, (a, b) -> b, (b1, b2) -> b1 + b2);

  System.out.println("Sum of all values: " + addition);

 }
}

Description of the above code:

  • First, the elements are inserted in the “ConcurrentHashMap” by utilizing the “put()” method.
  • Next, the “reduce()” method is used. The first parameter is the value of “parallelismThreshold”.
  • The second parameter consists of the “transformer” which transforms the output of the “reduce()” method to display only “values”.
  • Next to it, the reducer expression is attached that gets the “values” and returns the result after summing them.
  • In the end, the output is displayed on the screen.

After the end of the execution phase, the output is shown below:

The output shows that the sum of all values of the elements is printed on the console.

Conclusion

There are three Bulk ConcurrentHashMap operations in Java, namely the “forEach()”, “search()” and “reduce()” methods. The “forEach()” method iterates/traverses over each element and retrieves the “key”, “value” or “key/value” of that specific element according to a specified condition. The “search()” method traverses through Map and finds the specific element according to the expression which is inserted as a “transformer”. In the end, the “reduce()” method selects all elements and applies some task or operation to all of them at once like “subtraction”, “division”, etc.

Share Button

Source: linuxhint.com

Leave a Reply