Implement equals, hashcode in User

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (!(obj instanceof User))
            return false;
        User u = (User) obj;
        return this.empCode == null ? false : this.empCode
                .equals(u.empCode);
    }

    @Override
    public int hashCode() {
        return this.empCode == null ? 0 : this.empCode.hashCode();
    }

    @Override
    public String toString() {
        return "Emp Code: " + this.empCode;
    }

Then use retainAll

list2.retainAll(list1);-->EMP01, EMP02, EMP09, EMP10
Answer from Amit Deshpande on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ comparing-two-arraylist-in-java
Comparing two ArrayList In Java - GeeksforGeeks
January 23, 2026 - In Java, an ArrayList is used to store an ordered collection of elements. To compare two ArrayList objects, Java provides the equals() method.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-compare-two-arraylist-in-java
How to compare two ArrayList in Java
We have compared these ArrayLists using contains() method and stored the comparison result in third ArrayList (al3 and al4). package beginnersbook.com; import java.util.ArrayList; public class Details { public static void main(String [] args) { ArrayList<String> al1= new ArrayList<String>(); ...
Discussions

Comparing two Arraylists
If you would like to compare the items of two lists. You can create a loop and then you can compare each item step by step. If you would like to compare two lists independently from each other. You can use Java Comparator class with sort function. You can define code logic which has sorting rules. With this way, you can compare the fields of class whatever you want. I hope this is your expected answer ? More on reddit.com
๐ŸŒ r/javahelp
5
3
January 25, 2018
arraylist - How to compare two array lists for similar objects which differ in at least one property in java? - Stack Overflow
I have two array list. Each has list of Objects of type User. The User class looks like below public class User { private long id; private String empCode; private String first... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Comparing two object arraylists - Stack Overflow
First time here so I hope this makes sense! I have two object arrays say l1 and l2, I want to run a compare between these two lists and get a the unmatched value in say in l3. User class conta... More on stackoverflow.com
๐ŸŒ stackoverflow.com
A question about the comparison of elements in two ArrayLists in java.
I know that == can only be used to compare values, or Primitive types, while .equals() is used to compare references. Yes, you can use == to compare two int values, but you must use equals() to compare two Integer objects. ArrayLists contain Integers, and not int. This is why their type is ArrayList -- they are a list of Integer objects. If you have two Integer objects, you should use equals to compare the two. If you use ==, it will sometimes evaluate to true, and sometimes evaluate to false. It is best not to rely on the nuances of this behavior. If you really want to know, Integers with values between -128 and 127 are typically cached -- see https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(int) See https://www.online-java.com/DIQTngFfxJ as an example More on reddit.com
๐ŸŒ r/learnprogramming
3
1
April 21, 2023
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ comparing-two-arraylist-in-java
Check two ArrayList for equality in Java
June 18, 2024 - Two ArrayList can be compared to check if they are equal or not using the method java.util.ArrayList.equals(). This method has a single parameter i.e. an ArrayList that is compared with the current object.
๐ŸŒ
Javaprogramto
javaprogramto.com โ€บ 2020 โ€บ 04 โ€บ how-to-compare-two-arraylist-for-equality-in-java.html
How to compare two ArrayList for equality in Java 8? ArrayList equals() or containsAll() methods works?
June 13, 2021 - how to compare two lists in java using equals() and containsAll() method. These two methods take List as an argument and compare each and every object are same in each list. equals() method is overridden in ArrayList class.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 638779 โ€บ java โ€บ Compare-Contrast-ArrayList-Object
Compare and Contrast Two ArrayList<Object> (Java in General forum at Coderanch)
August 29, 2014 - There are 3 pieces of information you eventually want: - Added objects (exist in List 1 but not in List 2). - Removed objects (exist in List 2 but not in List 1). - Changed objects (exist in both lists, but one or more of their fields have changed). The first two are solved with a utility method that can compare two lists and return the difference of them.
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-compare-two-arraylist-in-java
How to Compare Two ArrayList in Java - Javatpoint
How to Compare Two ArrayList in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ a question about the comparison of elements in two arraylists in java.
r/learnprogramming on Reddit: A question about the comparison of elements in two ArrayLists in java.
April 21, 2023 -

I'm trying to do my homework

I created two ArraLists of Integer, and the content in the list is as follows:

List1:

5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150

List2:

150 300 450 600 750

when i use

list1. get(list1. size() - 1) == list2. get(0);

I get false;

I have to use

list1.get(list1.size() - 1).equals(list2.get(0));

to get true;

Then I try the following code:

ArrayList<Integer> arrList1 = new ArrayList<Integer>();

ArrayList<Integer> arrList2 = new ArrayList<Integer>();

arrList1. add(10);

arrList2.add(10);

System.out.println(arrList1.get(0) == arrList2.get(0));

I get true;

I know that == can only be used to compare values, or Primitive types, while .equals() is used to compare references.

I'm sure what I'm storing in the ArrayList is integer, int, so why can't I use == for comparison? (And I'm also wondering why comparing 10 with == gets true and 150 gets false)

I wonder why this is happening?

Thanks in advance to everyone who helped me.

๐ŸŒ
YouTube
youtube.com โ€บ naveen automationlabs
#7 - How to Compare Two ArrayLists in Java - YouTube
In this video, I have explained how to compare two ArrayLists in Java.Learn:1. Compare two ArrayList using Sort and Equals methods.2. Find out the additional...
Published: October 19, 2020
Views: 52K
๐ŸŒ
DevQA
devqa.io โ€บ java-compare-two-arraylists
Java Compare Two Lists
March 17, 2020 - To compare two lists for equality just in terms of items regardless of their location, we need to use the sort() method from the Collections() class. ... import java.util.Arrays; import java.util.Collections; import java.util.List; public class ...
๐ŸŒ
Medium
medium.com โ€บ @QA-initi โ€บ how-to-compare-two-array-list-in-java-using-comparator-cc05617175b5
How to compare two array list in Java using Comparator | by QA-init | Medium
November 14, 2023 - In order to compare two ArrayList in Java using a comparator, we can use the Collections.sort() method and pass in the ArrayList and a custom comparator.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ compare two lists in java: equality, differences, common items
Compare Two Lists in Java: Equality, Differences, Common Items
1 week ago - List<String> warehouse = List.of("apple", "banana", "cherry"); List<String> shop = List.of("cherry", "banana", "date"); List<String> reordered = List.of("banana", "cherry", "apple"); boolean equal = warehouse.equals(shop); // false boolean sameIgnoringOrder = warehouse.stream().sorted().toList() .equals(reordered.stream().sorted().toList()); // true List<String> onlyInWarehouse = new ArrayList<>(warehouse); onlyInWarehouse.removeAll(shop); // [apple] List<String> missingInWarehouse = new ArrayList<>(shop); missingInWarehouse.removeAll(warehouse); // [date] List<String> common = new ArrayList<>
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-compare-two-arraylist-for-equality-in-java
How to compare two ArrayList for equality in Java?
July 30, 2019 - You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 673893 โ€บ java โ€บ Comparing-ArrayLists-objects-shared-properties
Comparing two ArrayLists containing different objects with (some) shared properties (Beginning Java forum at Coderanch)
December 15, 2016 - The current solution I have in mind is to simply iterate through the two ArrayLists at the same time (after sorting them), comparing the shared properties, and if they match up, continue the loop. Please let me know if anything needs clarification, I'm new to all this.
๐ŸŒ
Apps Developer Blog
appsdeveloperblog.com โ€บ home โ€บ java โ€บ java examples โ€บ compare two arraylist in java
Compare two ArrayList in Java - Apps Developer Blog
August 16, 2022 - The ArrayList.equals() method compares two ArrayList. It compares the sizes of the lists, elements equality, and the order of elements. class Test { public static void main(String[] args) { List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); // list2 is equal to list1 List<Integer> ...
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ programming โ€บ java
How to compare two arraylist in java? - Raspberry Pi Forums
https://www.java8net.com/2020/03/compare-two-arraylist-in-java.html ... Hello, You can compare two array lists using the equals method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not ...