Skip to content
Related Articles

Related Articles

Improve Article

Vector subList() Method in Java

  • Difficulty Level : Basic
  • Last Updated : 05 Oct, 2021

The java.util.Vector.subList() is used to return a sublist of the existing Vector within a range mentioned in the parameter. The method takes in an upper limit and a lower limit and returns all the elements mentioned in the range. The lower limit is included if the element is present in the list and the upper limit is excluded. Basically, it takes the sublist greater than equal to the lower limit and strictly less than the upper element.

Syntax: 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Vector.subList(int low_index, int up_index)

Parameters: The method accepts 2 mandatory parameters: 

  • low_index: This is of the integer type and defines the lower limit or the index of the starting element from which the subList is evaluated. This element is included in the sublist.
  • up_index: This is of the integer type and defines the upper limit or the index of the end element till which the subList is evaluated. This element is excluded from the sublist.

Return Value: The method returns a sublist of the Vector type mentioned within the given range of the parameters.



Example 1:

Java




// Java Program to illustrate subList() method
// of Vector class
 
// Importing required class
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty Vector by declaring
        // object of Vector class of Integer type
        Vector<Integer> vec_tor = new Vector<Integer>();
 
        // Adding custom input elements
        // using add() method
        vec_tor.add(5);
        vec_tor.add(1);
        vec_tor.add(50);
        vec_tor.add(10);
        vec_tor.add(20);
        vec_tor.add(6);
        vec_tor.add(20);
        vec_tor.add(18);
        vec_tor.add(9);
        vec_tor.add(30);
 
        // Print and display all elements present in vector
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating the sublist vector
        List<Integer> sub_list = new ArrayList<Integer>();
 
        // Limiting the values till 5
        // using subList() method via passing arguments
        sub_list = vec_tor.subList(2, 5);
 
        // Displaying the elements present inside list
        System.out.println("The resultant values "
                           + "within the sub list: "
                           + sub_list);
    }
}
Output: 
The Vector is: [5, 1, 50, 10, 20, 6, 20, 18, 9, 30]
The resultant values within the sub list: [50, 10, 20]

 

Example 2:

Java




// Java Program to illustrate subList() method
// of Vector class
 
// Importing required class
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty Vector by
        // creating string object of Vector class
        Vector<String> vec_tor = new Vector<String>();
 
        // Adding custom input elements to above vector
        // object using add() method
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geek");
        vec_tor.add("For");
        vec_tor.add("Geeks");
 
        // Display message only
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating the sublist vector
        List<String> sub_list = new ArrayList<String>();
 
        // Limiting the values till 5
        // using subList() method
        sub_list = vec_tor.subList(1, 5);
 
        // Display elements of List object
        System.out.println("The resultant values "
                           + "within the sub list: "
                           + sub_list);
    }
}
Output
The Vector is: [Welcome, To, Geek, For, Geeks]
The resultant values within the sub list: [To, Geek, For, Geeks]



My Personal Notes arrow_drop_up
Recommended Articles
Page :