











Java Object equals()


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.
Object equals() method.
In Java, the equals method is used to determine if two objects are equal. The method is defined in the Object class, which is the superclass of all classes in Java. By default, the equals method compares the memory addresses of the two objects, which is not usually what is desired.
When overriding the equals method, the following guidelines should be followed:
- The equals method should be reflexive: for any non-null reference value x, x.equals(x) should return true.
- The equals method should be symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
Syntax:
public boolean equals(Object obj)
Parameters:
The merge() method in Java’s HashMap class takes three parameters:
- key: The key of the entry you want to insert or merge. It is of type K, where K is the type of keys in the HashMap.
- value: The value of the entry you want to insert or merge. It is of type V, where V is the type of values in the HashMap.
- remappingFunction: A BiFunction that takes in the current value associated with the key and the new value that you are trying to insert or merge. This function is used to compute the new value for the key. The function takes two parameters of type V and returns a value of type V.
Let’s look at a object-related Java program where the Java object equals Method is used to perform an operation on the given object.
Example: Java Object equals() Method
class Main { public static void main(String[] args) { // equals() with String objects // create objects of string String obj1 = new String(); String obj2 = new String(); // check if obj1 and obj2 are equal System.out.println(obj1.equals(obj2)); // true // assign values to objects obj1 = "PrepInsta "; obj2 = "PrepInsta Prime"; // again check if obj1 and obj2 are equal System.out.println(obj1.equals(obj2)); // false } }
Output
true false
Example 2 : Java object equals() Method
public class Main { public static void main(String[] args) { // get an integer, which is an object Integer x = new Integer(50); // get a float, which is an object as well Float y = new Float(50f); // check if these are equal,which is // false since they are different class System.out.println("" + x.equals(y)); // check if x is equal with another int 50 System.out.println("" + x.equals(50)); } }
Output
false true
Example 3: how to use the object equals property:
import java.util.HashMap; public class Main { public static void main(String[] args) { HashMapmap1 = new HashMap<>(); map1.put("A", 1); map1.put("B", 2); //map1 -> {"A": 1, "B": 2} HashMap map2 = new HashMap<>(); map2.put("B", 3); map2.put("C", 4); //map2 -> {"B": 3, "C": 4} //Merge map2 into map1 map2.forEach((key, value) -> map1.merge(key, value, Integer::sum)); //map1 -> {"A": 1, "B": 5, "C": 4} System.out.println(map1); } }
Output
{A=1, B=5, C=4}
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