Open In App

How to Find a Sublist in a List in Java?

Last Updated : 22 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

List in Java contains index-based methods. This enables us to maintain the order collection. So this enables us to search, insert, delete, and even update the elements. This enables us to store multiple copies of the same element already existing on our list. Also, in addition, null elements are allowed to be a part of the List.

We access the list through their index numbers called Interface that we won’t be discussing here.

Types of List

ArrayList is used where elements to be inserted are known because there is no flexibility once we have declared the ArrayList but is frequently used as operation over elements are much faster and the good part of Arraylist is we can directly access the element through ArrayList interface.

The syntax for ArrayList :

ArrayList<String> cars = new ArrayList<String>();

 LinkedList is preferred over Arraylist if we want a flexible list with no size constraint and operations over elements are quite slower. 

LinkedList<E> extends AbstractList<E> implements List<E>, Deque<E> ;

Vector method is similar to Arraylist just Vector has an edge over ArrayList because all elements in vectors are synchronized and are only useful if making multithreaded applications. So, in practice vector class isnt used more frequently anymore. 

Vector object= new vector(datatype parameter1, datatype parameter2, ...., datatype parameterN)

Sublist is a portion of List

The subList() method of java.util.ArrayList class is used to return a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all the optional list operations.

Syntax:

public List subList(int fromIndex, int toIndex)

Parameters: This method takes the following argument as a parameter.

  • fromIndex: low endpoint (inclusive) of the subList
  • toIndex: high endpoint (exclusive) of the subList

Return type: A view of the specified range within this list.
 
Exception: This method throws the following Exception.

  • IndexOutOfBoundsException – if an endpoint index value is out of range (fromIndex size)
  • IllegalArgumentException – if the endpoint indices are out of order (fromIndex > toIndex)

Example 1:

Java




// Java Program to find 
// Sublist in a List 
import java.util.*; 
  
public class GFG1 { 
    
    // Main Method
    public static void main(String[] argv) throws Exception 
    
  
        // Try block for exception
        try
  
            ArrayList<Integer> 
                arrlist = new ArrayList<Integer>(); 
  
            // Populating arrlist1 
            arrlist.add(1); 
            arrlist.add(4); 
            arrlist.add(9); 
            arrlist.add(25); 
            arrlist.add(36); 
  
            // Print arrlist 
            System.out.println("Original arrlist: "
                            + arrlist); 
  
            // Getting the subList 
            // using subList() method 
            List<Integer> arrlist2 = arrlist.subList(2, 4); 
  
            // Print the subList 
            System.out.println("Sublist of arrlist: "
                            + arrlist2); 
        
  
        // Catch block for exception
        catch (IndexOutOfBoundsException e) 
        
            System.out.println("Exception thrown : " + e); 
        
        
        // Catch block for exception
        catch (IllegalArgumentException e) 
        
            System.out.println("Exception thrown : " + e); 
        
    
}


Output:

Original arrlist: [1, 4, 9, 25, 36]
Sublist of arrlist: [9, 25]

Example 2:

Java




// Java program to find
// sublist in a List 
  
import java.util.*; 
  
public class GFG1 
{
    // Main Method 
    public static void main(String[] argv) throws Exception 
    
       // Exception try-catch block
        try
  
            ArrayList<String> arrlist = new ArrayList<String>(); 
  
            // Populating arrlist1 
            arrlist.add("Example"); 
            arrlist.add("in"); 
            arrlist.add("Geeks"); 
            arrlist.add("for"); 
            arrlist.add("Geeks"); 
  
            // print arrlist 
            System.out.println("Original arrlist: "
                            + arrlist); 
  
            // Getting the subList 
            // using subList() method 
            List<String> arrlist2 = arrlist.subList(2, 5); 
  
            // print the subList 
            System.out.println("Sublist of arrlist: "
                            + arrlist2); 
            
  
        // Exception try-catch block
        catch (IndexOutOfBoundsException e) 
        
            System.out.println("Exception thrown : " + e); 
        
  
        // Exception try-catch block
        catch (IllegalArgumentException e)
        
            System.out.println("Exception thrown : " + e); 
        
    


Output : 

Original arrlist: [Example, in, Geeks, for, Geeks]
Sublist of arrlist: [Geeks, for, Geeks]

Example 3: For IllegalArgumentException

Java




// Java program to demonstrate 
// subList() method 
// for IllegalArgumentException 
  
import java.util.*; 
  
public class GFG1
{
    // Main Method
    public static void main(String[] argv) throws Exception 
    
        
        // Exception try-catch block
        try
  
            ArrayList<String> arrlist = new ArrayList<String>(); 
  
            // Populating arrlist1 
            arrlist.add("Example"); 
            arrlist.add("in"); 
            arrlist.add("Geeks"); 
            arrlist.add("for"); 
            arrlist.add("Geeks"); 
  
  
            // Print arrlist 
            System.out.println("Original arrlist: "
                            + arrlist); 
  
            // Getting the subList 
            // Using subList() method 
            System.out.println("\nEndpoint indices "
                            + "are out of order"
                            + " (fromIndex > toIndex)"); 
            List<String> arrlist2 = arrlist.subList(9, 3); 
  
            // print the subList 
            System.out.println("Sublist of arrlist: "
                            + arrlist2); 
             
        
        // Exception try-catch block
        catch (IndexOutOfBoundsException e) 
        
            System.out.println("Exception thrown: " + e); 
        
  
        // Exception try-catch block
        catch (IllegalArgumentException e) 
        
            System.out.println("Exception thrown: " + e); 
        
        
    
    
}


Output:

Original arrlist: [Example, in, Geeks, for, Geeks]

Endpoint indices are out of order (fromIndex > toIndex)
Exception thrown: java.lang.IllegalArgumentException: fromIndex(9) > toIndex(3)

Example 4: For IndexOutOfBoundsException

Java




// Java program to demonstrate subList() 
// method for IndexOutOfBoundsException 
  
import java.util.*; 
  
public class GFG1 
    // Main Method
    public static void main(String[] argv) throws Exception 
    
        
        // Exception try-catch block
        try
  
            ArrayList<Integer> arrlist = new ArrayList<Integer>(); 
  
            // Populating arrlist1 
            arrlist.add(1); 
            arrlist.add(4); 
            arrlist.add(9); 
            arrlist.add(25); 
            arrlist.add(36); 
  
            // Print arrlist 
            System.out.println("Original arrlist: " + arrlist); 
  
            // Getting the subList 
            // Using subList() method 
            System.out.println("\nEnd index value is out of range"); 
            List<Integer> arrlist2 = arrlist.subList(2, 7); 
  
            // Print the subList 
            System.out.println("Sublist of arrlist: " + arrlist2); 
        
          
        // Exception try-catch block
        catch (IndexOutOfBoundsException e) 
        
            System.out.println("Exception thrown : " + e); 
        
  
        // Exception try-catch block
        catch (IllegalArgumentException e)
        
            System.out.println("Exception thrown : " + e); 
        
        
    
    
}


Output:

Original arrlist: [1, 4, 9, 25, 36]

End index value is out of range
Exception thrown : java.lang.IndexOutOfBoundsException: toIndex = 7


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads