In this article, we will discuss an example on how to search whether particular element present in ArrayList or NOT
1. Searching element from ArrayList using contains() method :
Method signature:
boolean contains(Object o);
- This method is used to search specified object from invoking collection
- Returns true, if present; otherwise return false
SearchElementFromArrayList.java
package in.bench.resources.java.collections;
import java.util.ArrayList;
public class SearchElementFromArrayList {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> al = new ArrayList<String>();
// adding elements to ArrayList object
al.add("Sundar Pichai");
al.add("Satya Nadella");
al.add("Shiv Nadar");
al.add("Shantanu Narayen");
al.add("Sundar Pichai"); // duplicate object
al.add("Francisco D’Souza");
al.add(null); // null is allowed
// Iterating using for-loop
System.out.println("Iterating using enhanced for-loop\n");
for(String founder : al) {
System.out.println(founder);
}
// searching element
boolean boolElement = al.contains("Satya Nadella");
System.out.println("\n\nWhether element Satya Nadella' "
+ "is present : " + boolElement);
// searching and printing in same line
System.out.println("\n\nWhether element 'Francisco D’Souza' "
+ "is present : " + al.contains("Francisco D’Souza"));
// searching and printing in same line
System.out.println("\n\nWhether element 'Narayan Murthy' "
+ "is present : " + al.contains("Narayan Murthy"));
}
}
Output:
Iterating using enhanced for-loop
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null
Whether element 'Satya Nadella' is present : true
Whether element 'Francisco D’Souza' is present : true
Whether element 'Narayan Murthy' is present : false
From above example, ArrayList
- allows duplicate elements
- null object is allowed
- while iterating insertion order is maintained
Related Articles:
- Java – Creating ArrayList using nCopies method of Collections class
- Java – How to get size or length of an ArrayList ?
- Java – Adding element to ArrayList at specified index position
- Java – Remove element from ArrayList at specified index position
- Java – How to delete a element and delete all elements of an ArrayList ?
- Java- How to get sublist from ArrayList ?
- Java – How to check whether particular element is present in ArrayList ?
- Java – Comparing two ArrayList objects using containsAll() method
- Java – Adding one ArrayList to another ArrayList using addAll() method
- Java – Replacing ArrayList element with new value using set() method
- Java – Various ways to iterate through ArrayList
- Java – How to reverse ArrayList contents ?
- Java – Remove duplicate elements from ArrayList
- Java – How to remove duplicate elements of ArrayList maintaining insertion-order ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html
Happy Coding !!
Happy Learning !!