Java8新特性——stream流

时间:2023-12-23 14:01:14

一、基本API初探

package java8.stream;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream; /**
* @author jiaqing.xu@hand-china.com
* @version 1.0
* @name
* @description
* @date 2018/7/15
*/
public class BasicTest {
public static void main(String[] args) { //创建串行流
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
filtered.forEach(System.out::println); //使用foreach进行数据迭代 limit 方法用于获取指定数量的流
Random random = new Random();
random.ints().limit(10).forEach(System.out::println); //Map用于映射每个元素对应的结果,原值为i 映射到i*i .collect(Collectors.toList()):将stream再转换回list集合
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().limit(2).collect(Collectors.toList());
squaresList.forEach(System.out::println); //filter 方法用于通过设置的条件过滤出元素
List<String> stringList = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
Long count = stringList.stream().filter(string -> string.isEmpty()).count();
System.out.println("The count of empty string:"+count); //sorted用于对流进行排序,默认是从小到大
List<Integer> array = Arrays.asList(1,3,2,4);
List<Integer> sortedList = array.stream().sorted().collect(Collectors.toList());
sortedList.forEach(System.out::println); //流并行处理程序parallelStream
List<String> stringList2 = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl","");
// 获取空字符串的数量
Long count2 = stringList2.parallelStream().filter(string -> string.isEmpty()).count();
System.out.println("The count of empty string:"+count2); //collectors 可以返回列表或者字符串
List<String> stringList3 = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl","");
String mergedString = stringList3.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString); //
int[] numberList = {12,3,34,67,100,99}; IntStream intStream = IntStream.of(numberList);
IntSummaryStatistics stats = intStream.summaryStatistics(); System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());
}
}

二、分组和合并

 /**
* @author jiaqing.xu@hand-china.com
* @version 1.0
* @name
* @description 分组、合并测试dto
* @date 2018/7/15
*/
public class Foo { private int code; private int count; public Foo(int code, int count) {
this.code = code;
this.count = count;
} public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
} @Override
public String toString() {
return "Foo{" +
"code=" + code +
", count=" + count +
'}';
}
}
/**
* @author jiaqing.xu@hand-china.com
* @version 1.0
* @name
* @description 测试分组和合并 groupingBy方法以及reduce方法
* @date 2018/7/15
*/
public class TestFoo {
public static void main(String[] args) {
Foo foo1 = new Foo(1, 2);
Foo foo2 = new Foo(2, 23);
Foo foo3 = new Foo(2, 6);
List<Foo> list = new ArrayList<>(4);
list.add(foo1);
list.add(foo2);
list.add(foo3);
Map<Integer, List<Foo>> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode));
//存储最后的汇总结果集合
List<Foo> result = new ArrayList<>();
collect.forEach((k,v)->{
Optional<Foo> sum = v.stream().reduce(
(v1, v2) -> { //合并
v1.setCount(v1.getCount()+v2.getCount());
return v1;
}
);
result.add(sum.orElse(new Foo(0,10)));
});
result.forEach(System.out::print); }
}

Java8新特性——stream流Java8新特性——stream流

将2号记录的count值进行了合并汇总!23+6=29