Splitting List into sublists along elements

后端 未结 13 1946
野趣味
野趣味 2020-11-28 21:51

I have this list (List):

[\"a\", \"b\", null, \"c\", null, \"d\", \"e\"]

And I\'d like something like this:

13条回答
  •  悲哀的现实
    2020-11-28 22:52

    I was watching the video on Thinking in Parallel by Stuart. So decided to solve it before seeing his response in the video. Will update the solution with time. for now

    Arrays.asList(IntStream.range(0, abc.size()-1).
    filter(index -> abc.get(index).equals("#") ).
    map(index -> (index)).toArray()).
    stream().forEach( index -> {for (int i = 0; i < index.length; i++) {
                        if(sublist.size()==0){
                            sublist.add(new ArrayList(abc.subList(0, index[i])));
                        }else{
    
                        sublist.add(new ArrayList(abc.subList(index[i]-1, index[i])));
                        }
                    }
        sublist.add(new ArrayList(abc.subList(index[index.length-1]+1, abc.size())));
    });
    

提交回复
热议问题