
- 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 search in a List of Java object?
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.
Java List interface provides two methods, indexOf() method which can be used to get the location of an element in the list and lastIndexOf() method which can be used to get the last occurence of an element. In this article, we're discussing both indexOf() and lastIndexOf() method to search an element in the list.
indexOf() method.
int indexOf(Object o)
Notes
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
Parameters
o - Element to search for.
Returns
The index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Throws
ClassCastException - If the type of the specified element is incompatible with this list (optional).
NullPointerException - If the specified element is null and this list does not permit null elements (optional).
lastIndexOf() method.
int lastIndexOf(Object o)
Notes
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
Returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
Parameters
o - Element to search for.
Returns
The index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
Throws
ClassCastException - If the type of the specified element is incompatible with this list (optional).
NullPointerException - If the specified element is null and this list does not permit null elements (optional).
Example 1
Following is the example showing the usage of indexOf() method −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0)); System.out.println("List: " + list); int positionOfFive = list.indexOf(Integer.valueOf(5)); System.out.println("5 is present at index: " + positionOfFive); int positionOfTen = list.indexOf(Integer.valueOf(10)); System.out.println("10 is present at index: " + positionOfTen); } }
Output
This will produce the following result −
List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] 5 is present at index: 4 10 is present at index: -1
Example 2
Following is the example showing the usage of lastIndexOf() method −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5)); System.out.println("List: " + list); int positionOfFive = list.lastIndexOf(Integer.valueOf(5)); System.out.println("5 is present at index: " + positionOfFive); int positionOfTen = list.lastIndexOf(Integer.valueOf(10)); System.out.println("10 is present at index: " + positionOfTen); } }
Output
This will produce the following result −
List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5] 5 is present at index: 14 10 is present at index: -1
- Related Articles
- Java Program to Search User Defined Object From a List By using Binary Search Comparator
- How to search for element in a List in Java?
- How do I search a list in Java?
- How to fill multiple copies of specified Object to a List in Java
- Search elements in a sorted object array in Java
- How to search in array of object in MongoDB?
- Java Program to Search an Element in a Circular Linked List
- How an iterator object can be used to iterate a list in Java?
- How to search for an item in a Lua List?
- How to search a file in a directory in java
- How to implement the search functionality of a JTable in Java?
- How to search for a pattern in a Java string?
- How to copy a list to another list in Java?
- What does the method search(Object o) do in java?
- Search in a SortedList object in C#
