List sublist() Method in Java with Examples Last Updated : 19 Jun, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report This method gives a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. Syntax: List subList(int fromIndex, int toIndex) Parameters: This function has two parameter fromIndex and toIndex, which are the starting and ending ranges respectively to create a sublist form the given list. Returns: This method returns the view of list between the given ranges. Below programs show the implementation of this method. Program 1: Java // Java code to show the implementation of // lastIndexOf method in list interface import java.util.*; public class GfG { // Driver code public static void main(String[] args) { // Initializing a list of type Linkedlist List<Integer> l = new LinkedList<>(); l.add(1); l.add(3); l.add(5); l.add(7); l.add(3); System.out.println(l); System.out.println(l.lastIndexOf(3)); } } Output: [1, 3, 5, 7, 3] 4 Output: [1, 3, 5, 7, 3] 4 Program 2: Below is the code to show implementation of list.subList() using Linkedlist. Java // Java code to show the implementation of // subList method in list interface import java.util.*; public class GfG { // Driver code public static void main(String[] args) { // Initializing a list of type Linkedlist List<Integer> l = new LinkedList<>(); l.add(10); l.add(30); l.add(50); l.add(70); l.add(30); List<Integer> sub = new LinkedList<>(); System.out.println(l); sub = l.subList(1, 3); System.out.println(sub); } } Output: [10, 30, 50, 70, 30] [30, 50] Reference: Oracle Docs Comment More infoAdvertise with us Next Article Stack subList() method in Java with Example B barykrg Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions java-list +1 More Practice Tags : JavaJava-Collections Similar Reads List size() method in Java with Examples The size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container.Example:Java// Java Program to demonstrate // List size() Method import java.util.*; class GFG { public sta 2 min read List add() Method in Java with Examples The List add() method adds (appends) an element to a list. It can be used for both ArrayList and LinkedList. Example of List add() Method: Java // Java program to demonstrate // ArrayList usage import java.io.*; import java.util.ArrayList; import java.util.List; class GFG { public static void main ( 3 min read List get() method in Java with Examples The get() method of List interface in Java is used to get the element present in this list at a given specific index. Example:Java// Java Program to demonstrate List // get() Method import java.util.*; class Main { public static void main (String[] args) { // Create a List List<Integer> a=new 2 min read Stack subList() method in Java with Example The subList() method of Java.util.Stack class is used to return a view of the portion of this Stack between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned Stack is empty.) The returned Stack is backed by this Stack, so non-structural cha 3 min read List removeAll() method in Java with Examples This method is used to remove all the elements present in the collection from the specified list. Syntax: boolean removeAll(Collection c) Parameters: This method has only argument, collection of which elements are to be removed from the given list. Returns: This method returns True if elements are r 2 min read List retainAll() Method in Java with Examples This method is used to retain all the elements present in the collection from the specified collection into the list. Syntax: boolean retainAll(Collection c) Parameters: This method has only argument, collection of which elements are to be retained in the given list. Returns: This method returns Tru 2 min read Like