Use

ArrayList.contains("StringToBeChecked");

If the String is present in the ArrayList, this function will return true, else will return false.

Answer from Logan on Stack Overflow
🌐
Java67
java67.com › 2018 › 01 › 5-ways-to-compare-string-objects-in-java.html
5 ways to Compare String Objects in Java - Example Tutorial | Java67
If you want to ignore case, better use equalsIgnoreCase(). You can use compareTo() and compare() if you are interested in the relative ordering of string objects e.g. while sorting strings in the list.
🌐
Reddit
reddit.com › r/learnprogramming › [java] comparing what's in an array list to a string
r/learnprogramming on Reddit: [Java] Comparing what's in an array list to a string
September 23, 2022 -

I have a list,

List<Equipment> equipment = response.getEquipment();

public class Equipment {
    private String initial;
    private String number;
    // other attributes im not concerned with atm  

and I want to see check if the value of initial and number for each piece of equipment equipment.get(index).getInitial()

equipment.get(index).getNumber()

is in the String:

String params = "ABCD1234,DEFG5678"

If it is, remove from string or replace with ""

The end goal is to have the

String params = "the number/numbers and initial/initials not present in List<Equipment> equipment"

So if equipment contains 1 entry whose initial = "ABC" and number="123", and the String params = "ABC123,DEF456", the resulting String params should be = "DEF456".

I'm not sure if my question makes sense, please let me know if you need any more info.

🌐
Rosetta Code
rosettacode.org › wiki › Compare_a_list_of_strings
Compare a list of strings - Rosetta Code
January 5, 2026 - boolean isAscending(String[] strings) { String previous = strings[0]; int index = 0; for (String string : strings) { if (index++ == 0) continue; if (string.compareTo(previous) < 0) return false; previous = string; } return true; } Alternately, Works with: Java version 8 ·
Top answer
1 of 5
5

Perhaps the easiest solution would be to use two List<List<String>>s instead. Assuming the List implementations used extend AbstractList, using equals will give you the desired behavior. From the documentation for AbstractList.equals:

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.

You can easily wrap a String[] in a thin List<String> implementation that extends AbstractList by using Arrays.asList.

EDIT: Here's an example:

String[] array1 = {"1", "2", "3"};
String[] array2 = {"4", "7"};

String[] array3 = {"1", "2", "3"};
String[] array4 = {"4", "7"};

List<List<String>> lst1 = new ArrayList<>();
lst1.add(Arrays.asList(array1));
lst1.add(Arrays.asList(array2));

List<List<String>> lst2 = new ArrayList<>();
lst2.add(Arrays.asList(array3));
lst2.add(Arrays.asList(array4));

System.out.println(lst1.equals(lst2)); //prints true
2 of 5
3

Typically you should avoid dealing with Arrays. they are ugly and lead to these kind of problems. If possible use List<List<String>> then you can use .equals() normally.

if you insist, you could use a custom isequal implementation like below. the key is to use Arrays.equals()

public class DemoEquals {
    List<String[]> listOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
    List<String[]> listOneOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
    List<String[]> listTwo = (List<String[]>) Arrays.asList(new String[]{"2one1", "2one2"}, new String[]{"2two1"});

    private boolean isEqual(List<String[]> list1, List<String[]> list2) {
        if (list1.size() != list2.size()) return false;
        for (int i = 0; i < list1.size(); i++) {
            if (!Arrays.equals(list1.get(i), list2.get(i))) return false;
        }
        return true;
    }
    @SuppressWarnings("unchecked")
    private void isEqual() {
        //prints true
        System.out.println(isEqual(Collections.EMPTY_LIST, Collections.EMPTY_LIST));
        //prints true
        System.out.println(isEqual(listOne, listOne));
        //prints true
        System.out.println(isEqual(listOne, listOneOne));
        //prints false
        System.out.println(isEqual(listOne, listTwo));
        //prints false
        System.out.println(isEqual(listOne, Collections.EMPTY_LIST));

    }
    public static void main(String[] args) {
        new DemoEquals().isEqual();
    }
}
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - Using the “==” operator for comparing text values is one of the most common mistakes Java beginners make. This is incorrect because “==” only checks the referential equality of two Strings, meaning if they reference the same object or not. ... String string1 = "using comparison operator"; String string2 = "using comparison operator"; String string3 = new String("using comparison operator"); assertThat(string1 == string2).isTrue(); assertThat(string1 == string3).isFalse();
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › how to compare two lists in java
How to Compare Two Lists in Java - HowToDoInJava
September 20, 2023 - List<String> listOfCommonItems = (List<String>) CollectionUtils.intersection(listTwo, listOne); Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("a", "b"), listOfCommonItems)); Happy Learning !! ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
🌐
Coderanch
coderanch.com › t › 648745 › java › compare-ArrayLists-type-ArrayList-String
how to compare two ArrayLists of type ArrayList ? (Java in General forum at Coderanch)
You could have an outer loop that loops through text and an inner loop that loops through orgId and inside that, compare the two items to see if they are equal. can you show me how to do it with example code..i have been trying but i am not able to get into the if statement. I am able to loop..but if i place a print statement inside if loop to check, i am not getting the output. -Thanks · JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... You can also go through the methods of the List interface and you will probably find exactly what you want already there.
🌐
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 - 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. package com.tuturself; import java.util.Arrays; import java.util.List; public class ListCompare { public static void main(String[] args) { List < Integer > aList = Arrays.asList(new Integer[] { 1, 3, 5, 6, 8 }); List < Integer > bList = Arrays.asList(new Integer[] { 10, 89, 8, 9 }); for (Integer i: aList) { if (bList.contains(i)) { System.out.println("Match Found " + i); break; } } } }
🌐
CodingTechRoom
codingtechroom.com › question › compare-string-arraylist-java
How to Compare a String Value with an ArrayList of Strings in Java - CodingTechRoom
Utilize the `contains()` method of ArrayList to check for the presence of a String value. Implement a loop to iterate through the ArrayList and compare each element using the `equals()` method. Consider using Java 8 Streams for a more modern approach to perform the comparison.
🌐
W3Docs
w3docs.com › java
Java Compare Two Lists
You can also use the containsAll() method to check if one list contains all the elements of the other list. Here is an example: List<String> list1 = Arrays.asList("a", "b", "c"); List<String> list2 = Arrays.asList("a", "b", "c", "d"); if ...
🌐
InfoWorld
infoworld.com › home › blogs › java challengers
String comparisons in Java | InfoWorld
September 5, 2025 - The String pool solves this problem ... at the following code sample. (Recall that we use the “==” operator in Java to compare two objects and determine whether they are the same.)...