1. Introductions
In previous articles, we saw how to create an ArrayList, how to add elements into ArrayList, and how to remove elements from ArrayList.
In this article, we will discuss how to check if an element exists in ArrayList.
2. Content
List interface provides 4 different APIs to check if and where the element exists in ArrayList.
Below are those APIs.
int indexOf(Object o) int lastIndexOf(Object o) boolean contains(Object o) boolean containsAll(Collection<?> c)
3. int indexOf(Object o)
indexOf(Object o) method is used to search for a specified element in ArrayList and return its index. If it doesn’t find the element, then it returns -1.
indexOf() method searches element from the 0th index to ArrayList’s size. As soon as it finds an element, it returns the index, i.e. returns the index of the first occurrence.
The worst-case time complexity of this method is O(n) where n is the size of ArrayList.
Let us understand this with examples.
Example 1: Search for the name John. name=John exists so the indexOf method will return its index as 0.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("Jack"); int index = names.indexOf("John"); Assert.assertEquals(0, index);
Example 2: Search for the name Sansa. As for the name, “Sansa” doesn’t exist so indexOf method will return -1.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("Jack"); int index = names.indexOf("Sansa"); Assert.assertEquals(-1, index);
Example 3: Search for the name John. Name John appears twice in ArrayList but the indexOf method will return the first occurrence, which is index = 0.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("John"); int index = names.indexOf("John"); Assert.assertEquals(0, index);
4. int lastIndexOf(Object o)
lastIndexOf method returns the last index of the specified object in the ArrayList. This method scans for the required element from the end of the ArrayList. If the element is found, it returns the index else returns -1.
The worst-case time complexity of this method is O(n) where n is the size of ArrayList.
Let us understand this with examples.
Example 1: Search for the name John. name=John exists so the lastIndexOf method will return its index as 0.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("Jack"); int index = names.lastIndexOf("John"); Assert.assertEquals(0, index);
Example 2: Search for the name Sansa. As the name Sansa doesn’t exist, so the lastIndexOf method will return -1.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("Jack"); int index = names.lastIndexOf("Sansa"); Assert.assertEquals(-1, index);
Example 3: Search for the name John. Name John appears twice in ArrayList but the lastIndexOf method will return the last occurrence, which is index = 2.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("John"); int index = names.lastIndexOf("John"); Assert.assertEquals(2, index);
5. boolean contains(Object o)
contains(Object o) method is used to check if the specified object exists in ArrayList or not.
If the object exists, then the method returns true, else it returns false.
The worst-case time complexity of this method is O(n) where n is the size of ArrayList.
Example :
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("Jack"); boolean containsJack = names.contains("Jack"); boolean containsSansa = names.contains("Sansa"); Assert.assertTrue(containsJack); Assert.assertFalse(containsSansa);
6. boolean containsAll(Collection<?> c)
containsAll(Collection<?> c) method is used to check whether all elements of a specified Collection exist in ArrayList or not.
The worst-case time complexity of this method is O(m.n) where n is the size of ArrayList and m is the size of Collection. Why O(m.n)? Because for every element that exists in Collection, it must exist in ArrayList. So for every element in Collection, ArrayList does a linear scan to check if this element is in ArrayList.
Example 1 :
List<String> names1 = new ArrayList<>(); names1.add("John"); names1.add("Jane"); names1.add("Jack"); List<String> names2 = new ArrayList<>(); names2.add("Jack"); names2.add("John"); Assert.assertTrue(names1.containsAll(names2)); Assert.assertFalse(names2.containsAll(names1));
Example 2: If the Collection has repeated values, then the count of the repeated values is not considered. For example, names2 ArrayList has size = 4, but one element is repeated 4 times. names1.containsAll(names2) will return true even though Jack is not repeated 4 times in names1.
List<String> names1 = new ArrayList<>(); names1.add("John"); names1.add("Jane"); names1.add("Jack"); List<String> names2 = new ArrayList<>(); names2.add("Jack"); names2.add("Jack"); names2.add("Jack"); names2.add("Jack"); Assert.assertTrue(names1.containsAll(names2)); Assert.assertFalse(names2.containsAll(names1));
7. Conclusion
In this article, we saw 4 different methods that check for the existence of the element in ArrayList.
- int indexOf(Object o): returns first occurrence index of the object if it exists else returns -1.
- int lastIndexOf(Object o): returns first occurrence index of the object if it exists else returns -1.
- boolean contains(Object o): returns true if the object exists in ArrayList, else returns false.
- boolean containsAll(Collection<?> c): returns true if the Collections exists in ArrayList else returns false.