我有两个测试类:
import java.util.Arrays;
import java.util.List;
import java.util.function.ToIntFunction;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class Age{
private int age;
public Age(int age){
this.age = age;
}
public int getAgeValue(){
return this.age;
}
}
("unused")
class User{
private int id;
private String name;
private Age age;
public User(int id, String name, int age){
this.id = id;
this.name = name;
this.age = new Age(age);
}
public Age getAge(){
return this.age;
}
public int getAgeWrapper(){
return this.age.getAgeValue();
}
}
先构造一个集合Collections:
private static List<User> users = Arrays.asList(
new User(1, "张三", 1),
new User(2, "李四", 2),
new User(3,"王五", 3),
new User(4, "赵六", 4));
// 获得一个并行流:
Stream userStream = users.parallelStream();
// 获得一个映射函数,该函数配合stream使用,能把所有User实例的Age字段映射成IntStream
ToIntFunction mapper = User::getAgeWrapper;
IntStream intstream = userStream.mapToInt(mapper);
// 得到输出:10
System.out.println("Total: " + intstream.sum());
如果重复调用sum:
System.out.println("Total2: " + intstream.sum());
会遇到错误消息:
Exception in thread “main” java.lang.IllegalStateException: stream has already been operated upon or closed
at java.base/java.util.stream.AbstractPipeline.eval(AbstractPipeline.java:229)
at java.base/java.util.stream.IntPipeline.reduce(IntPipeline.java:491)
at java.base/java.util.stream.IntPipeline.sum(IntPipeline.java:449)
at java8.stream.SumTest.main(SumTest.java:68)
如果不想用stream自带的sum方法,也可以手动用reduce计算:
int total = intstream.reduce(0, (x, y) -> x + y);
一样能得到10的结果。
全部测试代码:
package java8.stream;
import java.util.Arrays;
import java.util.List;
import java.util.function.ToIntFunction;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class Age{
private int age;
public Age(int age){
this.age = age;
}
public int getAgeValue(){
return this.age;
}
}
("unused")
class User{
private int id;
private String name;
private Age age;
public User(int id, String name, int age){
this.id = id;
this.name = name;
this.age = new Age(age);
}
public Age getAge(){
return this.age;
}
public int getAgeWrapper(){
return this.age.getAgeValue();
}
}
public class SumTest {
private static List<User> users = Arrays.asList(
new User(1, "张三", 1),
new User(2, "李四", 2),
new User(3,"王五", 3),
new User(4, "赵六", 4));
public static String covert2SnakeCase(final String camelCase)
{
final String regex = "([a-z])([A-Z])";
final String replacement = "$1_$2";
return camelCase.replaceAll(regex, replacement).toLowerCase();
}
public static void main(String[] args) {
//double sum = users.parallelStream().mapToInt(User::getAge).getAgeValue().reduce(0, (x, y) -> x + y);
double sum2 = users.parallelStream().mapToInt(User::getAgeWrapper).sum();
double sum = 0;
Stream<User> userStream = users.parallelStream();
ToIntFunction<User> mapper = User::getAgeWrapper;
IntStream intstream = userStream.mapToInt(mapper);
int total = intstream.reduce(0, (x, y) -> x + y);
System.out.println("sum: " + total);
// System.out.println("Total: " + intstream.sum());
//System.out.println("Total2: " + intstream.sum());
System.out.println("sum1: " + sum);
//System.out.println(" sum2: " + sum2);
System.out.println(covert2SnakeCase("helloJerry"));
System.out.println(covert2SnakeCase("CongratulationsJerry"));
}
}