Java Utility Library

Java Hashtable - replaceAll() Method



The java.util.Hashtable.replaceAll() method is used to replace each entry's value in this hashtable with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

Syntax

public void replaceAll(BiFunction<? super K,? super V,? extends V> function)

Here, K and V are the type of key and value respectively maintained by the container.


Parameters

function Specify the function to apply to each entry.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Hashtable.replaceAll() method is used to replace each entry's value in the given hashtable by invoking the given function.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a hashtable
    Hashtable<Integer, String> Htable = new Hashtable<Integer, String>();

    //populating the hashtable
    Htable.put(101, "John");
    Htable.put(102, "Marry");
    Htable.put(103, "Kim");
    Htable.put(104, "Jo");
    Htable.put(105, "Sam");

    //printing the content of the hashtable
    System.out.println("Htable contains: " + Htable);

    //replacing value of key with uppercase value
    Htable.replaceAll((k, v) -> v.toUpperCase());

    //printing the content of the hashtable
    System.out.println("Htable contains: " + Htable); 
  }
}

The output of the above code will be:

Htable contains: {105=Sam, 104=Jo, 103=Kim, 102=Marry, 101=John}
Htable contains: {105=SAM, 104=JO, 103=KIM, 102=MARRY, 101=JOHN}

❮ Java.util - Hashtable