Java HashMap merge()
What is Object in java.
In Java, an object is an instance of a class. A class is a blueprint or template for creating objects, and it defines the properties and methods of the objects created from it.The Object class is defined in the java.lang package and it is automatically imported in all Java programs.
An object has three characteristics:
- State: The data stored in the object, also known as its properties or attributes.
- Identity: A unique value that identifies the object, also known as its memory address.
- Behavior: The actions that the object can perform, also known as its methods.
To know more about Object toString in java read the complete article.
Java HashMap merge() method.
Java HashMap’s merge() method is used to merge a key-value mapping into the map, replacing the previous value if the key already exists. The method takes two arguments: the key and a BiFunction (a functional interface that takes two arguments and returns a result) that is used to compute the new value for the key. If the key is not already present in the map, the function is used to compute its value, which is then added to the map.
- Java HashMap’s merge() method is used to merge a key-value mapping into the map, replacing the previous value if the key already exists.
- The method takes two arguments: the key and a BiFunction (a functional interface that takes two arguments and returns a result) that is used to compute the new value for the key.
Syntax:
hashmap.merge(key, value, remappingFunction)
Parameters:
obj - the reference object with which to compare.
Let’s look at a object-related Java program where the Java Hashmap merge Method is used to perform an operation on the given object.
Example: Java HashMap Merge Method
import java.util.HashMap; public class Main { public static void main(String[] args) { HashMap< String, Integer > map = new HashMap<>(); map.merge("A", 1, Integer::sum); // Inserts new entry with key "A" and value 1 //map -> {"A": 1} map.merge("B", 2, Integer::sum); // Inserts new entry with key "B" and value 2 //map -> {"A": 1, "B": 2} System.out.println(map); } }
Output
{A=1, B=2}
Example 2 :Merge() method to insert an entry with a duplicate key into a Java HashMap
import java.util.HashMap; public class Main { public static void main(String[] args) { HashMapmap = new HashMap<>(); map.merge("A", 1, Integer::sum); // Inserts new entry with key "A" and value 1 //map -> {"A": 1} map.merge("A", 2, Integer::sum); // Inserts new entry with key "A" and value 2, but since key "A" already exist it will merge the value //map -> {"A": 3} System.out.println(map); } }
Output
{A=3}
Example 3: how to use the object equals property:
import java.util.Objects; public class Main { static class Temp{ private int a, b; public Temp(int a, int b) { this.a = a; this.b = b; } @Override public String toString() { return "Temp{" + "a=" + a + ", b=" + b + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Temp temp = (Temp) o; return a == temp.a && b == temp.b; } @Override public int hashCode() { return Objects.hash(a, b); } } public static void main(String[] args) { Temp temp1 = new Temp(2,3); Temp temp2 = new Temp(2,3); Temp temp3 = new Temp(5,4); System.out.println(Objects.equals(temp1, temp2)); System.out.println(Objects.equals(temp2, temp3)); } }
Output
true false
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others