How to Extract Some of the Elements From Given List in Java?

You are currently viewing How to Extract Some of the Elements From Given List in Java?

How to extract some of the elements from given list in Java?

import java.util.ArrayList;
import java.util.List;

public class ExArrayExtract {
  public static void main(String[] args) {
    // Create a list and add some elements to the list.
    List < String > list_Strings = new ArrayList < String > ();

    list_Strings.add("One");
    list_Strings.add("Two");
    list_Strings.add("Three");
    list_Strings.add("Four");
    list_Strings.add("Five");

    // print the original string.
    System.out.println("Original list: " + list_Strings);

    // string extracted from given index.
    List < String > sub_List = list_Strings.subList(0, 3);

    // print extracted string.
    System.out.println("List of first three elements: " + sub_List);
  }
}

Output

Original list: [One, Two, Three, Four, Five]
List of first three elements: [One, Two, Three]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply