Java Program to Access the Part of List as List
A List is an ordered sequence of elements stored together to form a collection. A list can contain duplicate as well as null entries. A list allows us to perform index-based operations, that is additions, deletions, manipulations, and positional access. Java provides an in-built interface <<java.util>> to perform list as well as other class-based functions.
Methods:
- The naive approach by maintaining the start and end index.
- Using subList() method.
Method 1
- Elements are accessed at the required indexes of the list.
- Add them to a new empty list created.
- The start and end index values are used to access the part of the list required. By default, the indexes are assumed to start at 0.
This approach requires an additional space of maintaining a new list to store the sublist desired.
Example
Java
// Java Program to Access the Part of List as List // Importing utility and input/output java classes import java.util.*; import java.io.*; // Importing Iterator class import java.util.Iterator; // Class public class GFG { // Main driver method public static void main(String[] args) { // Declaring a list List<Integer> list1 = new ArrayList<Integer>(); // Adding elements to list1 list1.add( 1 ); list1.add( 7 ); list1.add( 8 ); list1.add( 2 ); list1.add( 11 ); list1.add( 3 ); list1.add( 66 ); list1.add( 30 ); // Print the original List System.out.println( "The original list contents : " ); Iterator iterator = list1.iterator(); // Iterating over elements using hasNext() // which holds true till // there is further more element in the List while (iterator.hasNext()) { System.out.print(iterator.next() + " " ); } // Declaring a new List to store sublist List<Integer> new_list = new ArrayList<Integer>(); // Maintaining and Setting counter to zero int i = 0 ; // Specifying the positions of the list to access // custom int start_indx = 2 , end_indx = 5 ; // Condition check which holds true till // current counter value is less than size of List while (i < list1.size()) { // Checking if counter is in range of start and // end indx if (i >= start_indx && i <= end_indx) { // Adding element to new List new_list.add(list1.get(i)); } // Incrementing counter i++; } // Print all element of List System.out.println(); // Display message System.out.println( "The sublist contents : " ); // Iterator iterator = new_list.iterator(); // Iterating over elements using hasNext() method // which holds true till further element is // remaining in List else returns false while (iterator.hasNext()) { // Print the elements of subList System.out.print(iterator.next() + " " ); } } } |
The original list contents : 1 7 8 2 11 3 66 30 The sublist contents : 8 2 11 3
Time Complexity: O(n)
Space Complexity: O(n) where n is the size of the list.
Method 2: Java provides us with an in-built method sublist() to access the elements belonging to the specified range of index values. The method is provided by the ArrayList package.
Syntax :
public List subList(int fromIndex, int toIndex)
Parameters:
- fromIndex = start index
- toIndex = endIndex
Return Type: A list of the required elements. The method access all the elements in the range fromIndex to toIndex-1. If fromIndex is equal to toIndex, an empty list is returned.
Example:
Java
// Java Program to Access the Part of List as List // Importing java utility package, and // all classes of input/output library import java.util.*; import java.util.Iterator; import java.io.*; class AccessSublist { // Main driver method public static void main(String[] args) { // Declaring a List of String type List<String> list1 = new ArrayList<String>(); // Adding elements to above List(List1) // Custom inputs list1.add( "Are" ); list1.add( "you" ); list1.add( "working!" ); list1.add( "hard" ); list1.add( "Geeeks?" ); // Display message System.out.print( "The original list contents : " ); // Iterator Iterator iterator = list1.iterator(); // Iterating over elements using hasNext() // which holds true till there is // further more element present in List while (iterator.hasNext()) { // Print the original List System.out.print(iterator.next() + " " ); } // Extracting the contents of the list // index between 0 and 2 (custom) List<String> new_list = list1.subList( 0 , 3 ); // Print System.out.println(); // Display message System.out.print( "The sublist contents : " ); // Iterator iterator = new_list.iterator(); // Iterating over elements using hasNext() // which holds true till there is // further more element present in List while (iterator.hasNext()) { // Print the sublist(list2) System.out.print(iterator.next() + " " ); } } } |
The original list contents : Are you working! hard Geeeks? The sublist contents : Are you working!
Time Complexity: O(n) when n is no of elements in the list
Auxiliary Space: O(n)