Home > other >  Split a list into multiple sublists in Java
Split a list into multiple sublists in Java

Time:01-10

I have an input as below. This is a String.

[[1641771267063,-0.63,-0.35,-0.6,0.35],[1641771267315,-0.7,-1.56,-8.2,0.41],[1641771268065,-0.1,-0.89,-0.6,0.35],[1641771268451,-0.4,-0.35,-0.6,0.35]]

I need to split this into sub lists as [1641771267063,-0.63,-0.35,-0.6,0.35] and [1641771267315,-0.7,-1.56,-8.2,0.41] and so on.

My ultimate goal is to count the numbers inside each of these sets. It has to be 5 every time.

I attempted to write a snippet in Java as shown below. But the list isn't getting splitted. Please help.

List<String> inputList = new ArrayList(Arrays.asList(Input));
            int chunkSize = 1;
            List<List<String>> partitionedList = 
ListUtils.partition(inputList, chunkSize);

            for (List<String> list : partitionedList){
                System.out.println("print list " list);
                System.out.println("list size "  list.size());
            }

print list [[[1641771267063,-0.63,-0.35,-0.6,0.35],[1641771267315,-0.7,-1.56,-8.2,0.41],[1641771268065,-0.1,-0.89,-0.6,0.35],[1641771268451,-0.4,-0.35,-0.6,0.35]]]
list size 1

CodePudding user response:

Assuming that you treat your input as arrays of double, you can do the following:

double[][] input = {{1641771267063d, -0.63, -0.35, -0.6, 0.35}, {1641771267315d, -0.7, -1.56, -8.2, 0.41},
        {1641771268065d, -0.1, -0.89, -0.6, 0.35}, {1641771268451d, -0.4, -0.35, -0.6, 0.35}};

List<List<Double>> partitionedList = Arrays.stream(input)
        .map(subArray -> Arrays.stream(subArray).boxed().collect(Collectors.toList()))
        .collect(Collectors.toList());

for (List<Double> list : partitionedList) {
    System.out.println("print list "   list);
    System.out.println("list size "   list.size());
}

CodePudding user response:

Split a list into multiple subLists in Java

I am presuming from the title your source is a List of double values. But it is somewhat unclear because of the erratic placement of square([]) brackets in the list.

Here is one way. It uses the subList method of the List interface.

  • Use IntStream to generate the indices of the list, incrementing by chunckSize
  • Create a new list of the sublist (sublists are just a view of the original so a new list should be created).
  • The conditional for the 2nd object to subList ensures that an exception will not be thrown if the original list size is not a multiple of chuncksize. The last list in the list of lists would them be smaller than the rest.
// generate some data
List<Double> list = IntStream.range(1, 94)
        .mapToObj(i->(double)i)
        .toList();


int chunckSize = 8;
List<List<Double>> lists = IntStream
        .iterate(0, i -> i < list.size(),
                i -> i  = chunckSize)
        .mapToObj(i -> (List<Double>) new ArrayList<>(
                list.subList(i,
                        i   ((i < list.size() - chunckSize) ?
                                chunckSize :
                                list.size() - i))))
        .toList();

lists.forEach(System.out::println);

For this demo prints

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
[9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0]
[17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0]
[25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0]
[33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0]
[41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0]
[49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0]
[57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0]
[65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0]
[73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0]
[81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0]
[89.0, 90.0, 91.0, 92.0, 93.0]

If by some chance, your source data is a 2 dimensional double array you could convert it to a List as follows and then apply the above method.

double[][] vals = ....
List<Double> list = Arrays.stream(vals)
        .flatMapToDouble(Arrays::stream).boxed().toList();
  •  Tags:  
  • Related