Get Sublist from Vector

In the previous post, we have seen how to remove the element from a particular position. In this post, we will see how to get sublist from vector.

There is method ‘subList(int fromIndex,int toIndex)‘ which is used to get the sublist from vector.

Syntax:

public List<E> subList(int fromIndex,int toIndex)

It returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned List is empty.) The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa. The returned List supports all of the optional List operations supported by this List.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a List can be used as a range operation by operating on a subList view instead of a whole List. For example, the following idiom removes a range of elements from a List:
list.subList(from, to).clear();

Example:

Output:

All cars :[BMW, Honda, Audi, Ferrari, Bugatti]
Sublist from vector ‘car’ :[Honda, Audi]


 

Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Print Friendly, PDF & Email
Remove element at a specified index from Vector
Sort a Vector using Collections.sort()
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *