how to compare an object in arraylist code example

Example 1: array vs arraylist

Arrays are fixed size
ArrayList's size auotomatically adjusted
Arrays can hold primitives and object
ArrayList can hold only objects
Arrays can be multi dimensional
ArrayList cannot be multi-dimentional
Array is a build in data structure
ArrayList is implementing class of List interface in Collection framework

Example 2: how to compare two arraylists are equal in java

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
         
        ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
         
        Collections.sort(listOne);
        Collections.sort(listTwo);
         
        //Compare unequal lists example
         
        boolean isEqual = listOne.equals(listTwo);      //false
        System.out.println(isEqual);
         
        //Compare equals lists example
         
        ArrayList<String> listThree = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
         
        isEqual = listOne.equals(listThree);      //true
        System.out.println(isEqual);
    }
}

Tags:

Misc Example