how to compare string in arraylist oop code example
Example 1: ArrayList contains(Object o) method in java
import java.util.ArrayList;
public class ArrayListContainsMethodExample
{
public static void main(String[] args)
{
ArrayList<String> names = new ArrayList<String>(5);
names.add("virat");
names.add("dhoni");
names.add("rohit");
names.add("vijay");
names.add("ajay");
System.out.println("Names: " + names);
System.out.print("Does list contains name dhoni?: ");
System.out.println(names.contains("dhoni"));
System.out.print("Does list contains name Sudhakar?: ");
System.out.println(names.contains("Sudhakar"));
}
}
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);
boolean isEqual = listOne.equals(listTwo);
System.out.println(isEqual);
ArrayList<String> listThree = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f"));
isEqual = listOne.equals(listThree);
System.out.println(isEqual);
}
}