
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to find an element in a List with Java?
The List interface extends Collection interface and represents a collection storing a sequence of elements. User of a list has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most popular implementation of the List interface among the Java developers.
In Java List, there are couple of ways to find an element.
Use indexOf() method. - This method returns the index of the element if present otherwise -1.
Use contains() method. - This methods returns true if element is present otherwise false.
Loop through the elements of a list and check if element is the required one or not.
Loop through the elements of a list using stream and filter out the element.
In this article, we're going to cover each of the above methods mentioned in examples.
Example 1
Following is an example showing the usage of indexOf() and contains() methods to find an element in a list −
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Zara"); list.add("Mahnaz"); list.add("Ayan"); System.out.println("List: " + list); String student = "Ayan"; if(list.indexOf(student) != -1) { System.out.println("Ayan is present."); } if(list.contains(student)) { System.out.println("Ayan is present."); } } }
Output
This will produce the following result −
List: [Zara, Mahnaz, Ayan] Ayan is present. Ayan is present.
Example 2
Following is an example showing the usage of iteration and streams to find an element in a list −
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Zara"); list.add("Mahnaz"); list.add("Ayan"); System.out.println("List: " + list); String student = "Ayan"; for (String student1 : list) { if(student1 == "Ayan") { System.out.println("Ayan is present."); } } String student2 = list.stream().filter(s -> {return s.equals(student);}).findAny().orElse(null); System.out.println(student2); } }
Output
This will produce the following result −
List: [Zara, Mahnaz, Ayan] Ayan is present. Ayan
- Related Articles
- How do I find an element in Java List?
- How to find the last occurrence of an element in a Java List?
- How to remove an element from a Java List?
- How do you add an element to a list in Java?
- Java Program to remove an element from List with ListIterator
- How do I add an element to an array list in Java?
- How to find the index of given element of a Java List?
- Insert an element to List using ListIterator in Java
- How to search for element in a List in Java?
- How to find the element from a Python list with a maximum value?
- How to find the element from a Python list with a minimum value?
- How to check if Java list contains an element or not?
- How to find what is the index of an element in a list in Python?
- How to remove an element from a list in Python?
- How do you copy an element from one list to another in Java?
