Java8 之 NumbericStream

1、IntegerStream 转为 IntStream 

stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
int resultNumber = stream.mapToInt(i -> i.intValue()).filter(i -> i > 3).sum();
System.out.println(resultNumber);

2、IntStream  转为 IntegerStream 

int a = 9;
IntStream.rangeClosed(1, 100)
        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
        .boxed()
        .map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)})
        .forEach(r -> System.out.println("a=" + r[0] + ", b=" + r[1] + ", c=" + r[2]));

System.out.println();
IntStream.rangeClosed(1, 100)
        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
        .mapToObj(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)})
        .forEach(r -> System.out.println("a=" + r[0] + ", b=" + r[1] + ", c=" + r[2]));

你可能感兴趣的:(java-8)