Using Streams instead of for loop in java 8

2020-05-21 07:42发布

int [] numbers = {1,2,3,4,5,6,7,8};
int [] doubleNumbers = new int[numbers.length];
int [] tripleNumbers = new int[numbers.length];


for(int index = 0; index < numbers.length; index++)
{
    doubleNumbers[index] = numbers[index] * 2;  
    tripleNumbers[index] = numbers[index] * 3;
}

System.out.println("Double Numbers");
Arrays.stream(doubleNumbers).forEach(System.out::println);

System.out.println("Triple Numbers");
Arrays.stream(tripleNumbers).forEach(System.out::println);

I have above code where I have used for loop and double and triple the numbers and stored it in different arrays in single loop. Can anybody help me to write the same code using streams with its map and other methods without iterating numbers array twice.

4条回答
▲ chillily
2楼-- · 2020-05-21 08:09

Seems like your question is more complicated than just using Java Stream API. The better way is define some wrapper class like Num.class:

class Num {
    private final int value;

    //constructor

    public int powerOf(int index) {
        return Math.pow(value, index);
    }

}

Then you can just wrap your array elements into this object and call powerOf method where you need. With your implementation you are creating unnecessary arrays for keeping powered values. And using Stream API in this case is more convinient:

Arrays.stream(numbers).map(Num::new).forEach(n -> System.out.println("power of 2": + n.powerOf(2));
查看更多
Luminary・发光体
3楼-- · 2020-05-21 08:16

You can do it like this:

IntStream.range(0, numbers.length)
  .forEach(index -> {
    doubleNumbers[index] = numbers[index] * 2;
    tripleNumbers[index] = numbers[index] * 3;
  });
查看更多
我想做一个坏孩纸
4楼-- · 2020-05-21 08:24

You can use stream with forEach method to populate collections of doubles and triples e.g.:

public static void main(String[] args) {
    int [] numbers = {1,2,3,4,5,6,7,8};
    List<Integer> doubles = new ArrayList<Integer>();
    List<Integer> triples = new ArrayList<>();

    Arrays.stream(numbers)
    .boxed()
    .forEach(n -> {
        doubles.add(n*2);
        triples.add(n*3);
        }
    );

    System.out.println(doubles);
    System.out.println(triples);
}

Another example with map and collect:

public static void main(String[] args) {
    int [] numbers = {1,2,3,4,5,6,7,8};

    List<Integer> doubles = Arrays.stream(numbers)
    .boxed()
    .map(n -> n*2)
    .collect(Collectors.toList());

    List<Integer> triples = Arrays.stream(numbers)
            .boxed()
            .map(n -> n*3)
            .collect(Collectors.toList());

    System.out.println(doubles);
    System.out.println(triples);
}
查看更多
疯言疯语
5楼-- · 2020-05-21 08:31

You can apply the doubling and tripling within the stream:

public static void main(String[] args) {
    int [] numbers = {1,2,3,4,5,6,7,8};

    System.out.println("Double Numbers");
    Arrays.stream(numbers).map(x -> x*2).forEach(System.out::println);

    System.out.println("Triple Numbers");
    Arrays.stream(numbers).map(x -> x*3).forEach(System.out::println);
}

though technically that's still iterating over the array twice.

As a trick you could instead collect the results in a Map:

    Map<Integer, Integer> m = Arrays.stream(numbers)
        .boxed()
        .collect(Collectors.toMap(
            x -> x*2,
            x -> x*3
        ));
查看更多
登录 后发表回答