字符数组转换及数字求和 java8 lambda表达式 demo

public static void main(String[] args) throws IllegalAccessException {
//字符串转换为数字且每个加上100,输出。
String[] ids = {"100","12","23","45","68","78","99","200"};
List<Integer> list = Stream.of(ids).map(x -> Integer.valueOf(x) + 100).collect(Collectors.toList());
//编译循环
list.stream().forEach(x -> System.out.print(x+","));

//按1.8编译
Integer[] array = { 1, 2, 3, 4, 5 };
Stream.of(array).forEach(System.out::println); // prints 1, 2, 3, ...
// Arrays.stream(array).forEach(System.out::println);

//Mysql in 查询参数
String str = Stream.of(array).map(x -> "'"+String.valueOf(x)+"'").collect(Collectors.joining(",","(",")"));
System.out.println("str="+str);

//求和
// Integer sum = Stream.of(array).mapToInt(x -> x).sum();
// Integer sum = Stream.of(array).mapToInt(Integer::valueOf).sum();
Integer sum = Stream.of(array).mapToInt(x -> Integer.valueOf(x)).sum();
System.out.println("sum="+sum);

}

posted on 2020-07-03 08:46  oktokeep  阅读(1873)  评论(2编辑  收藏  举报