package me.ele.newretail.ecology.partner;
import com.alibaba.fastjson.JSON;
import lombok.experimental.Accessors;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author 雨火
*/
public class TestStream {
// 测试数据准备
static List<Data> testList1 = Stream.of(new Data().setId(1L).setName("名称1").setAge(11), new Data().setId(2L).setName("名称2").setAge(11), new Data().setId(3L).setName("名称3").setAge(33)).collect(Collectors.toList());
static List<Data> testList2 = Stream.of(new Data().setId(4L).setName("名称4").setAge(44)).collect(Collectors.toList());
static List<List<Data>> listList = Stream.of(testList1, testList2).collect(Collectors.toList());
@lombok.Data
@Accessors(chain = true)
private static class Data {
private Long id;
private String name;
private Integer age;
}
public static void main(String[] args) {
baseApi();
toCollection();
toMap();
groupingBy();
collectingAndThen();
}
private static void baseApi() {
// filter 过滤ID=1的元素
List<Data> filter = testList1.stream().filter(e -> e.getId().equals(1L)).collect(Collectors.toList());
System.out.printf("filter:%s%n", JSON.toJSONString(filter));
// map 将testList1中所有的元素的id取出来, 重新放进一个新的list
List<Long> map = testList1.stream().map(Data::getId).collect(Collectors.toList());
System.out.printf("map:%s%n", JSON.toJSONString(map));
// 将testList1和testList2中的所有元素取出来, 重新放在一个list里面
List<Data> flatMap = listList.stream().flatMap(Collection::stream).collect(Collectors.toList());
System.out.printf("flatMap:%s%n", JSON.toJSONString(flatMap));
// testList1的年龄相加
int sum1 = testList1.stream().mapToInt(Data::getAge).sum();
System.out.printf("testList1的年龄相加:%s%n", JSON.toJSONString(sum1));
// testList1年龄的平均值
double v = testList1.stream().mapToInt(Data::getAge).average().orElse(0);
System.out.printf("testList1年龄的平均值:%s%n", JSON.toJSONString(v));
// 计算个数
long count = testList1.stream().mapToInt(Data::getAge).count();
System.out.printf("计算个数:%s%n", JSON.toJSONString(count));
// 拆箱和包箱
List<Integer> collect = testList1.stream().mapToInt(Data::getAge).boxed().collect(Collectors.toList());
System.out.printf("拆箱和包箱:%s%n", JSON.toJSONString(collect));
// listList中的所有元素的年龄相加
long sum = listList.stream().flatMapToLong(e -> e.stream().mapToLong(Data::getAge)).sum();
System.out.printf("listList中的所有元素的年龄相加:%s%n", JSON.toJSONString(sum));
// 排序, 按照年龄排序
List<Data> collect1 = testList1.stream().sorted(Comparator.comparing(Data::getAge)).collect(Collectors.toList());
System.out.printf("按照年龄排序:%s%n", JSON.toJSONString(collect1));
List<Data> collect2 = testList1.stream().sorted(Comparator.comparing(Data::getAge).reversed()).collect(Collectors.toList());
System.out.printf("按照年龄排序倒叙:%s%n", JSON.toJSONString(collect2));
// 按照年龄正序, 在按照名字倒叙
List<Data> collect3 = testList1.stream().sorted(Comparator.comparing(Data::getAge).thenComparing(Data::getName).reversed()).collect(Collectors.toList());
System.out.printf("按照年龄正序, 在按照名字倒叙:%s%n", JSON.toJSONString(collect3));
// 按照年龄正序, 在按照名字倒叙
List<Data> collect4 = testList1.stream().sorted(((o1, o2) -> {
if (o1.getAge().compareTo(o2.getAge()) > 0) {
return 1;
} else if (o1.getAge().compareTo(o2.getAge()) == 0) {
if (o1.getName().compareTo(o2.getName()) == 0) {
return 0;
} else if (o1.getName().compareTo(o2.getName()) <= 0) {
return 1;
} else {
return -1;
}
} else {
return -1;
}
})).collect(Collectors.toList());
System.out.printf("按照年龄正序, 在按照名字倒叙:%s%n", JSON.toJSONString(collect4));
// testList1 将元素的每个字段都累加起来
Data data = testList1.stream().reduce((o1, o2) -> {
//
return new Data().setId(o1.getId() + o2.getId()).setName(o1.getName() + o2.getName()).setAge(o1.getAge() + o2.getAge());
}).orElse(null);
System.out.printf("将元素的每个字段都累加起来:%s%n", JSON.toJSONString(data));
}
private static void toCollection() {
List<Data> collect = testList1.stream().collect(Collectors.toList());
System.out.printf("toList:%s%n", JSON.toJSONString(collect));
ArrayList<Data> collect1 = testList1.stream().collect(Collectors.toCollection(() -> new ArrayList<>(testList1.size())));
System.out.printf("ArrayList:%s%n", JSON.toJSONString(collect1));
LinkedList<Data> collect2 = testList1.stream().collect(Collectors.toCollection(LinkedList::new));
System.out.printf("LinkedList:%s%n", JSON.toJSONString(collect2));
HashSet<Data> collect3 = testList1.stream().collect(Collectors.toCollection(() -> new HashSet<>()));
System.out.printf("HashSet:%s%n", JSON.toJSONString(collect3));
LinkedHashSet<Data> collect4 = testList1.stream().collect(Collectors.toCollection(() -> new LinkedHashSet<>()));
System.out.printf("LinkedHashSet:%s%n", JSON.toJSONString(collect4));
List<Data> collect5 = testList1.stream().collect(Collectors.toList());
System.out.printf("toList:%s%n", JSON.toJSONString(collect5));
Set<Data> collect6 = testList1.stream().collect(Collectors.toSet());
System.out.printf("toSet:%s%n", JSON.toJSONString(collect6));
}
private static void toMap() {
Map<Long, Data> collect = testList1.stream().collect(Collectors.toMap(Data::getId, e -> e));
System.out.printf("toMap:%s%n", JSON.toJSONString(collect));
Map<Long, Data> collect1 = testList1.stream().collect(Collectors.toMap(Data::getId, e -> e, (p, s) -> p));
System.out.printf("collect1:%s%n", JSON.toJSONString(collect1));
HashMap<Long, Data> collect2 = testList1.stream().collect(Collectors.toMap(Data::getId, e -> e, (p, s) -> p, () -> new HashMap<>((int) (testList1.size() / 0.75))));
System.out.printf("collect2:%s%n", JSON.toJSONString(collect2));
}
private static void groupingBy() {
Map<Long, List<Data>> collect = testList1.stream().collect(Collectors.groupingBy(Data::getId));
System.out.printf("groupingBy:%s%n", JSON.toJSONString(collect));
Map<Long, List<Data>> collect1 = testList1.stream().collect(Collectors.groupingBy(Data::getId, Collectors.toList()));
System.out.printf("groupingBy:%s%n", JSON.toJSONString(collect1));
Map<Long, LinkedList<Data>> collect2 = testList1.stream().collect(Collectors.groupingBy(Data::getId, Collectors.toCollection(LinkedList::new)));
System.out.printf("groupingBy:%s%n", JSON.toJSONString(collect2));
HashMap<Long, LinkedList<Data>> collect3 = testList1.stream().collect(Collectors.groupingBy(Data::getId, HashMap::new, Collectors.toCollection(LinkedList::new)));
System.out.printf("groupingBy:%s%n", JSON.toJSONString(collect3));
HashMap<Long, LinkedList<Data>> collect4 = testList1.stream().collect(Collectors.groupingBy(Data::getId, () -> new HashMap<>(64), Collectors.toCollection(LinkedList::new)));
System.out.printf("groupingBy:%s%n", JSON.toJSONString(collect4));
HashMap<Long, LinkedList<Data>> collect5 = testList1.stream().collect(Collectors.groupingBy(Data::getId, () -> new LinkedHashMap<>(64), Collectors.toCollection(LinkedList::new)));
System.out.printf("groupingBy:%s%n", JSON.toJSONString(collect5));
ConcurrentMap<Long, List<Data>> collect6 = testList1.stream().collect(Collectors.groupingByConcurrent(Data::getId));
System.out.printf("groupingBy:%s%n", JSON.toJSONString(collect6));
}
private static void collectingAndThen() {
List<Long> collect = testList1.stream().collect(Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().map(Data::getId).collect(Collectors.toList())));
System.out.printf("collectingAndThen:%s%n", JSON.toJSONString(collect));
}
}
java8-stream-lambda-表达式常用方法总结的
于 2023-07-10 14:43:45 首次发布