Recompute Values

Using compute() Method.

  1. compute() - Computes a new value using the specified function. It then associates the computed value to the specified key.
  2. computeIfAbsent() - If the specified key is not mapped to any value, the method will compute a new value using the specified function. It then associates the new value with the key.
  3. computeIfPresent() - If the specified key is already mapped to any value, this method will compute a new value using the specified function. It then associates the new value with the key.


\begin{lstlisting}
import java.util.HashMap;
\par
class Main {
public static vo...
...println(''HashMap using computeIfPresent(): '' + numbers);
}
}
\end{lstlisting}

Output


\begin{lstlisting}
Original HashMap : {Second =2 , First =1}
HashMap using compu...
...Map using computeIfPresent () : {Second =6, First =3, three =5}
\end{lstlisting}