How to get a sublist of Vector in Java
In this example we will see how to retrieve a list that contains a set of elements from Vector. We'll use the subList() of the Vector.method subList(int start, int end) returns a portion of the Vector list between start and fin. The resulting list is returned by the Vector list, so changes in this list will involve updates in the Vector list and vice versa. To check this, remove an item from the list and view the original list to see if it has been deleted.
This example shows how to retrieve a list of items from Vector. Creating a sublist implies that you need to:
1) Create a Vector
2) Add elements to Vector with the add(Element e)
3) invoke the subList(int start, int end). The method returns a list of objects containing elements from the beginning india to the fin-1 index of the original Vector list. The resulting list is
Let's take a look at the following code:
import java.util.Collections;
import java.util.List;
import java.util.Vector;
public class SubList {
public static void main(String[] args) {
//create vector
Vector< String> vector = new Vector< String> ();
//add elements to vector
vector.add("o1");
vector.add("o2");
vector.add("o3");
vector.add("o4");
vector.add("o5");
vector.add("o6");
System.out.println(vector);
//Create a sublist
List< String> list = vector.subList(2, 5);
Output:
System.out.println("subList(2, 5) : ");
for(String e:list)
System.out.println(e);
}
}
[o1, o2, o3, o4, o5, o6]
subList(2, 5):
o3
o4
o5