Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. list.contains() / indexOf() / subList()

list.contains() / indexOf() / subList()

Methods for checking whether a list contains a specific element, retrieving the position of an element, or extracting a portion of a list. Searching and sub-list access are fundamental list operations.

Syntax

// Returns whether the element is contained in the list.
list.contains(Object o);

// Returns the index of the first matching element.
list.indexOf(Object o);

// Returns the index of the last matching element.
list.lastIndexOf(Object o);

// Returns a view (sub-list) of the specified range.
list.subList(int fromIndex, int toIndex);

Method List

MethodDescription
contains(Object o)Returns true if the list contains the specified element, or false otherwise.
indexOf(Object o)Returns the index of the first occurrence of the specified element. Returns -1 if not found.
lastIndexOf(Object o)Returns the index of the last occurrence of the specified element. Returns -1 if not found.
subList(int fromIndex, int toIndex)Returns a view of the portion of the list from fromIndex up to, but not including, toIndex.

Sample Code

import java.util.ArrayList;
import java.util.Arrays;

ArrayList<String> fruits = new ArrayList<>(Arrays.asList("apple", "banana", "cherry", "banana"));

// Use contains() to check whether an element exists.
System.out.println(fruits.contains("banana")); // Prints "true".
System.out.println(fruits.contains("grape"));  // Prints "false".

// Use indexOf() to get the index of the first occurrence.
System.out.println(fruits.indexOf("banana"));  // Prints "1".
System.out.println(fruits.indexOf("grape"));   // Prints "-1".

// Use lastIndexOf() to get the index of the last occurrence.
System.out.println(fruits.lastIndexOf("banana")); // Prints "3".

// Use subList() to get a portion of the list.
java.util.List<String> sub = fruits.subList(1, 3); // Gets elements at index 1 and 2.
System.out.println(sub); // Prints "[banana, cherry]".

// subList() returns a view of the original list, so changes are reflected back.
sub.clear(); // Clearing the sub-list also removes those elements from the original list.
System.out.println(fruits); // Prints "[apple, banana]".

// Use containsAll() to check for multiple elements at once.
ArrayList<String> check = new ArrayList<>(Arrays.asList("apple", "grape"));
System.out.println(fruits.containsAll(check)); // Prints "false".

Notes

Both contains() and indexOf() scan from the beginning of the list internally, so they have O(n) time complexity. If you need to search large datasets frequently, consider using HashSet or HashMap instead.

subList() returns a view backed by the original list, not an independent copy. Changes to the sub-list (such as set() or clear()) are reflected in the original list. If you need an independent copy, save it as a new list using new ArrayList<>(list.subList(...)).

To add or retrieve elements, see add() / get(). To sort a list, see Collections.sort().

If you find any errors or copyright issues, please .