You can make your Student override the .equals(Object o) method.

public class Student {

    private int id;
    private int classId;
    private String name;
    private int age;

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (classId != other.classId)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }


}

Then you can compare two students like so:

Student s1 = ...;
Student s2 = ...;

if(s1.equals(s2)) {
   //Do this when they are equal
}

This is a clean way to check for equality and all of Java's functions will call this overrided equals method. You can also use it with lists.

List<Student> studentList = ...;
Student s1 = ...;

if(studentList.contains(s1)) {
   //Do this when s1 is in the list
}

Please let me know if you have any questions or if I misunderstood your question.

Answer from RareTyler on Stack Overflow
🌐
YouTube
youtube.com › java coding insight interview preparation
Comparing List of Objects Using Multiple Fields -Plain Java 8 Comparator - No Third Party Library - YouTube
If you find this video helpful, please 'Like' or 'Subscribe'. This is really helpful for the channel and also motivates me to do more of such good and usefu...
Published: September 30, 2021
Views: 5K
Discussions

java - How to compare a field between two Lists of objects? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
September 21, 2015
How to compare two different list object in java? - Stack Overflow
I have two class lists like List A1 and List b1 and both lists contain one field which is common. So by using this field I have to compare those two list? Please can any... More on stackoverflow.com
🌐 stackoverflow.com
Java Compare Two List's object values? - Stack Overflow
I have two list **ListA listA = new ArrayList ()** and ListB listB = new ArrayList () both contain object of type MyData and MyData contain these More on stackoverflow.com
🌐 stackoverflow.com
java - Determine if 2 lists with similar objects contain a partial duplicate - Code Review Stack Exchange
I have two lists of Person objects. I need to compare the lists to determine if items in list1 exist in list2 and vice versa. This is not a straight equals() scenario as the objects are not the sa... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
August 21, 2018
🌐
javaspring
javaspring.net › blog › java-compare-two-list-s-object-values
Java: How to Compare Two Lists of Objects by Name and Boolean Values – Step-by-Step Guide
For example: Person nullName1 = new Person(null, true); Person nullName2 = new Person(null, true); System.out.println(nullName1.equals(nullName2)); // Output: true · If lists have duplicates (e.g., two Alice (true) entries), the stream-based ...
Find elsewhere
Top answer
1 of 4
3

The approach, algorithm

To find if an object exists in a list, you need to perform a linear search, potentially visiting every single element, in \$O(n)\$ time. More efficient data structures exist:

  • Use an ordered data structure: if the values are sorted, then you can find if an element exists using binary search, in \$O(\log n)\$ time.

  • Use a hashset: you can find if an element is in the set in constant time, \$O(1)\$

To be to search efficiently, use a hashset instead of a list. However, to be able to use a hashset efficiently, it is required that the objects you put in it have appropriate implementation of hashCode and equals methods.

See the official tutorial on the Object class, especially the sections on the equals and hashCode methods. Note that IDEs like IntelliJ and Eclipse can generate these methods for you easily (they are boring to write by hand, and usually there's little reason to do so).

With correct implementation of the equals and hashCode methods, for example as in the other answer by @Teddy, your main program could be reduced to this:

public static void main(String[] args) {

    Set<Person> originalPeople = new HashSet<>();
    Set<Person> newPeople = new HashSet<>();

    originalPeople.add(new Person("William", "Tyndale"));
    originalPeople.add(new Person("Jonathan", "Edwards"));
    originalPeople.add(new Person("Martin", "Luther"));

    newPeople.add(new Person("Jonathan", "Edwards"));
    newPeople.add(new Person("James", "Tyndale"));
    newPeople.add(new Person("Roger", "Moore"));

    for (Person original : originalPeople) {
        if (!newPeople.contains(original)) {
            System.out.printf("%s %s is not in the new list!%n",
                    original.getFirstName(), original.getLastName());
        }
    }
}
2 of 4
3

There are certain reasons I would not prefer your approach. These are:

1) There are multiple method calls which reduces the readability.

2) You are using filter twice which decreases the performances. You could do it inside the same filter like I've shown below

3) With this approach you don't have a consolidated List which contains common elements. For that you need another else condition which increases the cyclomatic complexity.

4) A null check which could produce NPE if not handled carefully as you are using findFirst() which returns Optional (Although null can be replaced with default new Person("","") object which is again not recommended).

5) Finally don't use ArrayList<Person> originalPeople = new ArrayList<>();; rather declare as List, to follow programming to interface.

Rather I would use below approach:

private static List<Person> getPersonInList(
            final List<Person> newPeople, List<Person> originalPeople) {
        List<Person> list = new ArrayList<>();

        newPeople.forEach(p ->
                originalPeople.stream()
                              .filter(p1 -> p.getFirstName().equals(p1.getFirstName()) && 
                                      p.getLastName().equals(p1.getLastName()))
                              .forEach(list::add));
        return list;
    }
🌐
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 - Either the order differs, or the element class does not override equals(). Without the override, equals() compares object references, so two objects with the same fields are different.
🌐
CodingTechRoom
codingtechroom.com › question › -merge-lists-java
How to Merge Two Lists of Objects by Comparing Field Values in Java - CodingTechRoom
Merging two lists of objects based on a particular field involves comparing each object in the first list with every object in the second list. A common approach is to use a combination of Java's stream API and collectors or simple iteration ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-objects-by-multiple-fields-in-java
How to Compare Objects by Multiple Fields in Java? - GeeksforGeeks
July 23, 2025 - To compare objects by multiple ... Suppose you have a class MyClass with fields field1, field2, and field3. You want to compare objects of this class first by field1, then by field2, and finally by field3...
🌐
Stack Overflow
stackoverflow.com › questions › 40081708 › how-to-compare-fields-between-arrays-of-objects › 40081808
java - How to compare fields between arrays of objects? - Stack Overflow
let Object1 and Object2 implement the same Interface which declares a getter method in which each class returns its string. Then let both classes implement Comparable interface in which each class compares its string with the return value of the common interfaces getter method. ... Any reason why you can't iterate through 1 list, call the getter for the field and compare it with the corresponding object in the other list?
Top answer
1 of 1
1

Something like this. Feel free to ask questions.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class App6 {
    public static final String ADDED = "ADDED";
    public static final String DELETED = "DELETED";
    public static final String MODIFIED = "MODIFIED";
    public static final String NOT_MODIFIED = "NOT_MODIFIED";

    public static void main(String[] args) {
        List<A> originalObjectList = Arrays.asList(new A("a"), new A(""), new A("a"), new A("a"));
        List<A> modifiedObjectList = Arrays.asList(new A("a"), new A("b"), new A("b"), new A(""));

        List<Result> results = IntStream
                .range(0, originalObjectList.size())
                .mapToObj(i -> {
                    A original = originalObjectList.get(i);
                    A modified = modifiedObjectList.get(i);
                    return compare(original, modified);
                })
                .collect(Collectors.toList());
        System.out.println(results);
    }

    public static Result compare(A or, A mod) {
        if (or == null || mod == null) {
            throw new IllegalArgumentException("Comparable objects should not be null");
        }
        if (or.field == null || mod.field == null) {
            throw new IllegalArgumentException("Comparable fields of objects should not be null");
        }
        if (or.field.equals(mod.field)) {
            return new Result(NOT_MODIFIED, or.field);
        } else if (or.field.isEmpty() && !mod.field.isEmpty()) {
            return new Result(ADDED, mod.field);
        } else if (!or.field.isEmpty() && mod.field.isEmpty()) {
            return new Result(DELETED, or.field);
        } else {
            return new Result(MODIFIED, mod.field);
        }
    }
}

class A {
    String field;

    public A(String field) {
        this.field = field;
    }
}

class Result {
    String status;
    String value;

    public Result(String status, String value) {
        this.status = status;
        this.value = value;
    }

    @Override
    public String toString() {
        return "Result{" +
                "status='" + status + '\'' +
                ", value='" + value + '\'' +
                '}';
    }
}
🌐
Baeldung
baeldung.com › home › java › java list › finding the differences between two lists in java
Finding the Differences Between Two Lists in Java | Baeldung
April 29, 2026 - As an example, imagine we have a list of students who applied for an exam, and another list of students who passed it. The difference between those two lists would give us the students who didn’t pass the exam.
🌐
amitph
amitph.com › home › java › comparing two lists in java
Comparing Two Lists In Java - amitph
November 22, 2024 - There is no direct way to compare Java Lists to find their commonality or particularity in Java.
🌐
DZone
dzone.com › coding › languages › how to compare list objects in java 7 vs. java 8
How to Compare List Objects in Java 7 vs. Java 8 - DZone
June 1, 2018 - As an example, you have a List of Employee Objects and you need to check that all your Employees are above 18. One or more elements from one List match(es) the elements of another List.