Answers for "find sublists in a arraylist java"

CSS
1

find sublists in a arraylist java

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

public class SearchElement
{

    public static void main(String[] args) 
    {
        // List of String
        List itemList = Arrays.asList("123", "start", "data1", "data2", "start", "data3", "start", "start", "data4");
        List<List<String>> newList =  new ArrayList<List<String>>();

        // searchItem
        String searchItem = "start";
        
        System.out.println("Item list Size : " + itemList.size());
        System.out.println("Item list : " + itemList);

        int start, index  ; 
        // Itrate the list to get Sublist
        for(index = 0; index < itemList.size(); index++)
        {
            // Get last index of the serached Item
            start =itemList.lastIndexOf(searchItem);
            
            // Get last sublist
            List subList = itemList.subList(start, itemList.size());
            
            // Add the sublist to the newList on index 0
            newList.add(0, subList);
            
            // Exclude the sublist from the main item list
            itemList = itemList.subList(0, start);
                
        }
        
        // Check newList
        System.out.println("List of sublist : " + newList);
    
    }
}
Posted by: Guest on October-21-2021

Code answers related to "find sublists in a arraylist java"

Browse Popular Code Answers by Language