9

I have three lists,

List<Double> list1= new ArrayList(List.of(1, 1.5));
List<Double> list2= new ArrayList(List.of(30, 25));
List<Double> list3= new ArrayList(List.of(30, 25));

I want to traverse through each simultaneously, and print

1    30  30
1.5  25  25

How can I do this using java-8 stream API?

1
  • Reminder why for comprehensions are awesome. :) Commented Aug 28, 2018 at 14:03

4 Answers 4

4

List.of(..) is a static factory returning a List itself, there is no need to wrap it into a ArrayList::new constructor.

Here is the code you probably need, however, don't underestimate the power for a for-loop:

// Here you need to get the max of all the List sizes to traverse all Lists
int maxSize = Math.max(list1.size(), Math.max(list2.size(), list3.size()));      

IntStream.range(0, maxSize)                                    // Iterate 0 .. minSize-1
         .mapToObj(i -> Stream.of(list1, list2, list3)         // Map each index
                              .filter(list -> i < list.size()) // Secure the existing index
                              .map(list -> list.get(i))        // Get the item
                              .collect(Collectors.toList()))   // Collect to List
                              .forEach(System.out::println);   // And iterate (print)

Output:

[1, 30, 30]
[1.5, 25, 25]

In case of variable list size, the traversal iteration is safe because I touch only the existing indices.

Sign up to request clarification or add additional context in comments.

1 Comment

You could remove the filter entirely by using Math.min in the first place. Also collecting adds no real benefit. As you could just iterate over the stream with .forEach
3

You could do (assuming all lists have the same size):

IntStream.range(0, list1.size())
         .forEach(x -> Syso(list1.get(x) + " " + list2.get(x) + " " + list3.get(x)))

But there is no real benefit here, just use a plain for loop.

And btw don't wrap these with new ArrayList(List.of(30, 25));, this would be enough:

 List<Double> list1 = List.of(1D, 1.5D)

Comments

2

No need for streams - just transpose the lists:

private static class Transpose<T> extends AbstractList<List<T>> implements List<List<T>> {
    private final List<T> [] lists;
    public Transpose(List<T>... lists) {
        this.lists = lists;
    }

    private class Column extends AbstractList<T> implements List<T> {
        private final int column;

        public Column(int column) {
            this.column = column;
        }

        @Override
        public T get(int row) {
            return lists[row].get(column);
        }

        @Override
        public int size() {
            return lists.length;
        }
    }
    @Override
    public List<T> get(int column) {
        return new Column(column);
    }

    @Override
    public int size() {
        return lists[0].size();
    }
}

private void test() {
    List<Double> list1= Arrays.asList(1.0, 1.5);
    List<Double> list2= Arrays.asList(30.0, 25.0);
    List<Double> list3= Arrays.asList(30.0, 25.0);

    List<List<Double>> matrix = new Transpose(list1, list2, list3);
    System.out.println(matrix);
}

Prints:

[[1.0, 30.0, 30.0], [1.5, 25.0, 25.0]]

Obviously this would be more difficult if the lists aren't all the same length.

Comments

1

This works when the lists' size are same or different:

List<Double> list1 = List.of(1D, 1.5D);
List<Double> list2 = List.of(30D, 25D);
List<Double> list3 = List.of(30D, 25D);
Stream<List<Double>> listStream = Stream.of(list1, list2, list3);

int maxSize = listStream.mapToInt(List::size).max().orElse(0);

IntStream.range(0, maxSize)
        .forEach(index -> {
            listStream
                    .filter(list -> list.size() > index)
                    .forEach(list -> System.out.print(list.get(index) + " "));
            System.out.println();
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.