Question
stringlengths
1
113
Answer
stringlengths
22
6.98k
Can you provide an example of Java ConcurrentHashMap class: computeIfAbsent()?
//import statement import java.util.concurrent.*; import java.util.*; public class ConcurrentHashMapcomputeIfAbsentExample1_1 { public static void main(String[] args) { // crete a HashMap and add some values HashMap<String, Integer> mapcon = new HashMap<>(); mapcon.put("k1", 100); mapcon.put("k2", 200); mapcon.put("k3", 300); mapcon.put("k4", 400); System.out.println("HashMap values :\n " + mapcon.toString()); mapcon.computeIfAbsent("k5", k -> 200 + 300); mapcon.computeIfAbsent("k6", k -> 60 * 10); System.out.println("New HashMap after computeIfAbsent :\n "+ mapcon); } } Output: HashMap values : {k1=100, k2=200, k3=300, k4=400} New HashMap after computeIfAbsent : {k1=100, k2=200, k3=300, k4=400, k5=500, k6=600}
What is Java ConcurrentLinkedQueue Class?
ConcurrentLinkedQueue is an unbounded thread-safe queue which arranges the element in FIFO. New elements are added at the tail of this queue and the elements are added from the head of this queue.
What are the class methods of Java ConcurrentLinkedQueue?
Method: add() Description: Inserts the specified element at the tail of this queue Method: addAll() Description: Inserts all the elements which are present in the specified collection to the tail of this queue Method: contains() Description: Returns true if this queue contains the specified element Method: forEach() Description: Performs the given action for each element until all elements have been processed. Method: isEmpty() Description: Returns true if this queue contains no elements. Method: iterator() Description: Returns an iterator over the elements in this queue Method: offer() Description: Inserts the specified element at the tail of this queue Method: remove() Description: Removes the specified element from this queue, if this element is present in the queue Method: removeAll() Description: Removes all the elements of this in queue which are present in the specified collection. Method: removeIf() Description: Removes all the elements in this queue that satisfy the given predicate filter. Method: retainAll() Description: Retain only those elements in this queue that are present in the specified collection. Method: size() Description: Returns the number of the elements in this queue. Method: spliterator() Description: Returns a spliterator over the elements in this queue. Method: toArray() Description: Returns an array containing all the elements of this queue which are in proper sequence.