Vinicio López
Vinicio López

Reputation: 25

How can I make a list of sublists in this case...?

Let's say I have a list like this:

[a, b, c, d, e, f, PASS, g, h]

But I want that whenever the word "PASS" is present, make another list with the remaining data.

Like this:

[[a, b, c, d, e, f], [g, h]]

Upvotes: 1

Views: 78

Answers (5)

user4935529
user4935529

Reputation: 1

I arranged simple solution, check whether is this matching for you. Thank you.

/**
 * @author Duminda Hettiarachchi
*/


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

public class StringSubList {

ArrayList<String> subList;

public StringSubList() {
    subList = new ArrayList<String>();
}

public void subList(List<String> list) {

    int passIndex = list.indexOf("PASS");

    List<String> list1 = (List<String>) list.subList(0, passIndex);
    List<String> list2 = (List<String>) list.subList(passIndex+1, list.size());

    List<List<String>> subLists = new ArrayList<List<String>>();

    subLists.add(list1);
    subLists.add(list2);

    System.out.println("List 1 :" + subLists.get(0));
    System.out.println("List 2 : " + subLists.get(1));
}

public static void main(String[] args) {

    String mainArr[] = {"a", "b", "c", "d", "e", "f", "PASS", "g", "h"};

    List<String> myList = Arrays.asList("a", "b", "c", "d", "e", "f", "PASS", "g", "h");

    new StringSubList().subList(myList);       `enter code here`

    }
}

OUTPUT :

List 1 :[a, b, c, d, e, f]

List 2 : [g, h]

Upvotes: 0

Eugene
Eugene

Reputation: 120848

This can be written as a generic method (I had that already in my stackoverflow answers, so I guess I've read about this somewhere before... I'll try to find where specifically):

private static <T> List<List<T>> splitBy(List<T> list, T delimiter) {

    int[] indexes = IntStream.rangeClosed(-1, list.size())
            .filter(i -> i == -1 
                    || i == list.size()
                    || delimiter.equals(list.get(i))).toArray();

    return IntStream.range(0, indexes.length - 1)
            .mapToObj(x -> list.subList(indexes[x] + 1, indexes[x + 1]))
            // or since java-11, a bit nicer:
            // .filter(Predicate.not(List::isEmpty))
            .filter(l -> !l.isEmpty())
            .collect(Collectors.toList());
}

Upvotes: 1

dotnet-mystic
dotnet-mystic

Reputation: 11

You can start by making a startIndex and endIndex. Keep track of each index and when you see "PASS", just make a sublist of the list from startIndex to endIndex. Then, update your indexes accordingly.

public static List<List<String>> getSplittedList (List<String> li)
   {
    List<List<String>> lists = new ArrayList<>();
    int startIndex = 0;
    int endIndex = 0;
    for(int i = 0; i < li.size(); i++)
    {
        if(li.get(i).equals("PASS"))
        {
            if(startIndex < endIndex)
                lists.add(li.subList(startIndex, endIndex));
            startIndex = i+1;
            endIndex = i+1;
        }
        else
            endIndex++;
    }
    if(startIndex < li.size())
        lists.add(li.subList(startIndex, li.size()));
    return lists;
}

Upvotes: 0

Afaq Ahmed Khan
Afaq Ahmed Khan

Reputation: 2302

another solution to your case

public static void main(String[] args) {

    String[] ar = { "a", "b", "c", "d", "e", "f", "PASS", "g", "h" };

    ArrayList<String> al = new ArrayList<String>(Arrays.asList(ar));

    ArrayList<String> al1 = new ArrayList<String>(al.subList(0, al.indexOf("PASS")));

    ArrayList<String> al2 = new ArrayList<String>(al.subList(al.indexOf("PASS") + 1, al.size()));

    ArrayList<ArrayList<String>> listOLists = new ArrayList<ArrayList<String>>();
    listOLists.add(al1);
    listOLists.add(al2);

}

Upvotes: 0

ernest_k
ernest_k

Reputation: 45309

Here's a method that uses a loop to "split" a list of Comparables on a delimiter:

private static <T extends Comparable<T>> List<List<T>> split(
       List<T> original, T delimiter) {

    List<List<T>> res = new ArrayList<>();

    List<T> current = new ArrayList<>();
    for (T f : original) {
        if (f.compareTo(delimiter) == 0) {
            res.add(current);
            current = new ArrayList<>();
            continue;
        }

        current.add(f);
    }

    if (!current.isEmpty()) {
        res.add(current);
    }

    return res;
}

And it returns [[a, b, c, d, e, f], [g, h]] when tested with:

public static void main(String[] args) throws Exception {
    List<String> list = 
     Arrays.asList("a", "b", "c", "d", "e", "f", "PASS", "g", "h");
    System.out.println(split(list, "PASS"));
}

Upvotes: 0

Related Questions