EDIT

Here are two versions. One using ArrayList and other using HashSet

Compare them and create your own version from this, until you get what you need.

This should be enough to cover the:

P.S: It is not a school assignment :) So if you just guide me it will be enough

part of your question.

continuing with the original answer:

You may use a java.util.Collection and/or java.util.ArrayList for that.

The retainAll method does the following:

Retains only the elements in this collection that are contained in the specified collection

see this sample:

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public class Repeated {
    public static void main( String  [] args ) {
        Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
        Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));

        listOne.retainAll( listTwo );
        System.out.println( listOne );
    }
}

EDIT

For the second part ( similar values ) you may use the removeAll method:

Removes all of this collection's elements that are also contained in the specified collection.

This second version gives you also the similar values and handles repeated ( by discarding them).

This time the Collection could be a Set instead of a List ( the difference is, the Set doesn't allow repeated values )

import java.util.Collection;
import java.util.HashSet;
import java.util.Arrays;

class Repeated {
      public static void main( String  [] args ) {

          Collection<String> listOne = Arrays.asList("milan","iga",
                                                    "dingo","iga",
                                                    "elpha","iga",
                                                    "hafil","iga",
                                                    "meat","iga", 
                                                    "neeta.peeta","iga");

          Collection<String> listTwo = Arrays.asList("hafil",
                                                     "iga",
                                                     "binga", 
                                                     "mike", 
                                                     "dingo","dingo","dingo");

          Collection<String> similar = new HashSet<String>( listOne );
          Collection<String> different = new HashSet<String>();
          different.addAll( listOne );
          different.addAll( listTwo );

          similar.retainAll( listTwo );
          different.removeAll( similar );

          System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similar, different);
      }
}

Output:

$ java Repeated
One:[milan, iga, dingo, iga, elpha, iga, hafil, iga, meat, iga, neeta.peeta, iga]

Two:[hafil, iga, binga, mike, dingo, dingo, dingo]

Similar:[dingo, iga, hafil]

Different:[mike, binga, milan, meat, elpha, neeta.peeta]

If it doesn't do exactly what you need, it gives you a good start so you can handle from here.

Question for the reader: How would you include all the repeated values?

Answer from OscarRyz on Stack Overflow
Top answer
1 of 12
182

EDIT

Here are two versions. One using ArrayList and other using HashSet

Compare them and create your own version from this, until you get what you need.

This should be enough to cover the:

P.S: It is not a school assignment :) So if you just guide me it will be enough

part of your question.

continuing with the original answer:

You may use a java.util.Collection and/or java.util.ArrayList for that.

The retainAll method does the following:

Retains only the elements in this collection that are contained in the specified collection

see this sample:

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public class Repeated {
    public static void main( String  [] args ) {
        Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
        Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));

        listOne.retainAll( listTwo );
        System.out.println( listOne );
    }
}

EDIT

For the second part ( similar values ) you may use the removeAll method:

Removes all of this collection's elements that are also contained in the specified collection.

This second version gives you also the similar values and handles repeated ( by discarding them).

This time the Collection could be a Set instead of a List ( the difference is, the Set doesn't allow repeated values )

import java.util.Collection;
import java.util.HashSet;
import java.util.Arrays;

class Repeated {
      public static void main( String  [] args ) {

          Collection<String> listOne = Arrays.asList("milan","iga",
                                                    "dingo","iga",
                                                    "elpha","iga",
                                                    "hafil","iga",
                                                    "meat","iga", 
                                                    "neeta.peeta","iga");

          Collection<String> listTwo = Arrays.asList("hafil",
                                                     "iga",
                                                     "binga", 
                                                     "mike", 
                                                     "dingo","dingo","dingo");

          Collection<String> similar = new HashSet<String>( listOne );
          Collection<String> different = new HashSet<String>();
          different.addAll( listOne );
          different.addAll( listTwo );

          similar.retainAll( listTwo );
          different.removeAll( similar );

          System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similar, different);
      }
}

Output:

$ java Repeated
One:[milan, iga, dingo, iga, elpha, iga, hafil, iga, meat, iga, neeta.peeta, iga]

Two:[hafil, iga, binga, mike, dingo, dingo, dingo]

Similar:[dingo, iga, hafil]

Different:[mike, binga, milan, meat, elpha, neeta.peeta]

If it doesn't do exactly what you need, it gives you a good start so you can handle from here.

Question for the reader: How would you include all the repeated values?

2 of 12
45

You can try intersection() and subtract() methods from CollectionUtils.

intersection() method gives you a collection containing common elements and the subtract() method gives you all the uncommon ones.

They should also take care of similar elements

🌐
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. In Java, there’s no explicit way of finding the differences between two lists in the List API, though there are some helper methods that come close.
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
Compare two List<Map<String, Object>> to pull out matching records
Have you considered using a record to hold the information instead of a map? Would be much easier to do item-per-item comparison. More on reddit.com
🌐 r/javahelp
2
4
August 16, 2022
Java How to use streams to compare lists in two lists of lists instead of two 'for' loops?
A few points. This isn't a great sub for this sort of question. From the sidebar: Self-posts and Q&A threads are welcome, but we prefer high quality posts focused directly on graduate level CS material. r/learnprogramming or r/AskProgramming would be better I can't quite parse your question. It sounds like you are trying to compare two lists, but your lists are made up of nested lists. Streams can help you flatten those lists (via flatMap ), but AFAIK there's no Streams function in the standard library that operates on multiple Streams at once. You'd have to write that function yourself, or else get it from a third-party library like Guava. Why do you think that Streams would be more efficient than using for loops? What do you think the Streams API would do under the covers that would be more efficient than a for loop? More on reddit.com
🌐 r/compsci
1
0
March 19, 2019
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
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-equals-method-in-java-with-examples
List equals() Method in Java with Examples - GeeksforGeeks
January 3, 2025 - It compares the lists as, both lists should have the same size, and all corresponding pairs of elements in the two lists are equal. ... // Java code to show the implementation of // addAll method in list interface import java.util.*; public class Geeks { public static void main(String[] args) { // Initializing a list of type ArrayList List<Integer> l = new ArrayList<>(); l.add(1); l.add(2); // Initializing another list List<Integer> l2 = new ArrayList<>(); l2.add(1); l2.add(2); if (l.equals(l2)) System.out.println("Equal"); else System.out.println("Not equal"); } }
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Check If Two Lists Are Equal In Java - Java Code Geeks
March 20, 2019 - Note that we have created copies of the two lists to ensure that the elements in the original lists remain untouched. If the data in our lists are unique i.e. there isn’t a duplication, we can simply create TreeSets from the given lists and then compare them using equals():
🌐
Medium
donraab.medium.com › comparing-collections-in-java-69c7e70fc5f1
Comparing Collections in Java | Medium
May 2, 2024 - These two Assertions are very useful when it comes to comparing two Collections in Java. Developers who use the Java Collection Framework have three basic Collection types that they can use to compare for equality. These types are java.util.List, java.util.Set, and java.util.Map.
Find elsewhere
🌐
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 - Feel free to reference my mock table below: There is no way to check a modification vs an addition/removal (That I know even if I'm just eyeing the data versus java calculating it). It's impossible to check for changes since there is no unique key that can be compared across the two connectors. The difference function's purpose is not to find the changed objects, it's only for finding the added/removed ones. The changed objects are found by creating the two new lists and then comparing their contents; that was the point of creating copies and removing the added/removed objects.
🌐
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
September 24, 2018 - To compare lists ignoring order, we sort copies of both lists and compare the copies with equals(). Sorting keeps duplicates, so [a, a, b] and [a, b, b] stay different, which is the correct result.
🌐
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
June 1, 2018 - All elements of a List exist in another List. Now, developing these use cases is very easy in Java 7 with relatively few lines of code. The following is an example where we are comparing two Lists in Java 7 and checking if any element from List 1 exists in List 2.
🌐
GeeksforGeeks
geeksforgeeks.org › advance java › assert-two-lists-for-equality-ignoring-order-in-java
Assert Two Lists for Equality Ignoring Order in Java - GeeksforGeeks
July 23, 2025 - In Java, comparing two lists for equality typically checks both the content and the order of the elements using the equals() method.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
July 28, 2026 - The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.
🌐
Programiz
programiz.com › java-programming › examples
Java Examples | Programiz
Java Program to Join Two Lists · Java Program to Convert a List to Array and Vice Versa · Java Program to Get Current Working Directory · Java Program to Convert Map (HashMap) to List · Java Program to Convert Array to Set (HashSet) and Vice-Versa · Java Program to Convert Byte Array to Hexadecimal ·
🌐
Baeldung
baeldung.com › home › kotlin › kotlin collections › comparing two lists in kotlin
Comparing Two Lists in Kotlin | Baeldung on Kotlin
March 19, 2024 - A List is an ordered collection of elements. Further, a List can contain duplicate values. Therefore, when we’re talking about comparing two lists, depending on the requirement, there are a couple of scenarios in which we consider two lists are equal:
🌐
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. Here is an example: ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(3); ...
🌐
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... A multidimensional array is an array that contains other arrays. You can use it to store data in a table with rows and columns. To create a two-dimensional array, write each row inside its own curly braces:
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.15.0 documentation
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:
🌐
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>(); ...
🌐
LeetCode
leetcode.com › problems › merge-two-sorted-lists
Merge Two Sorted Lists - LeetCode
Can you solve this real interview question? Merge Two Sorted Lists - You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
🌐
Baeldung
baeldung.com › home › java › java collections › check if two lists are equal in java
Check if Two Lists Are Equal in Java Baeldung
January 5, 2024 - In this short article we’ll focus on the common problem of testing if two List instances contain the same elements in exactly the same order. List is an ordered data structure so the order of elements matters by design. have a look at an excerpt from the List#equals Java documentation:
🌐
amitph
amitph.com › home › java › finding the difference between two java lists
Finding the Difference Between Two Java Lists - amitph
November 22, 2024 - Set<String> differences = Sets .difference(new HashSet<>(one), new HashSet<>(two)); System.out.println(differences); //prints: //[Leo, Tim]Code language: Java (java) The difference method returns an instance of a Set containing the elements that exist only in the first instance and not in the other. This tutorial taught us how to find the difference between two Lists in Java.