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.
Returns returns the new value associated with the key.
Notes:The equals method for class Object implements the most discriminating possible equivalence relation on objects
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}
Explanation:In this example, the merge() method is used to insert two new entries into the map, one with key "A" and value 1, and another with key "B" and value 2. The Integer::sum function is used to compute the new value for the key. Since the keys "A" and "B" are not already present in the map, the function is not used and the new values are inserted directly into the map.
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) {
HashMap map = 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}
Explanation:In this example, the merge() method is used to insert an entry with key "A" and value 1 into the map, then it insert another entry with key "A" and value 2. Since key "A" already present in the map, the function Integer::sum is used to compute the new value for the key. it will merge the value by adding the existing value (1) and the new value (2) , the new value is 3.
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
Explanation:The equals() method implementation compares the fields a and b of the Temp object being passed as an argument to the current object's fields a and b. If both are equal, the method returns true, otherwise it returns false.
The main method creates 3 instances of the Temp class, assigns values to their fields and the Objects.equals() method compares the two instances passed as arguments and returns true if the fields are equal and false otherwise.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription