stream流的常见类型转换
/**
* stream流
* filter过滤
* distinct去重
*/
@Test
void stream1Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
List<String> distinct = strings.stream().distinct().collect(Collectors.toList());
System.out.println("distinct = " + distinct);
List<String> filter = strings.stream().filter(s -> s.contains("张")).collect(Collectors.toList());
System.out.println("filter = " + filter);
}
@Test
void stream2Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
List<BlackProductVo> blackProductVos = new ArrayList<>();
blackProductVos.add(new BlackProductVo("1234", "20220501", "20220601", "时间错误", "3", "F"));
blackProductVos.add(new BlackProductVo("1235", "20220501", "20220601", "天数错误", "3", "F"));
blackProductVos.add(new BlackProductVo("1234", "20220501", "20220601", "类型错误", "3", "F"));
List<BlackProductVo> distinct = blackProductVos.stream().distinct().collect(Collectors.toList());
System.out.println("distinct = " + distinct);
}
/**
* limit 获取指定数量的元素
*/
@Test
void stream3Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
List<String> limit = strings.stream().limit(2).collect(Collectors.toList());
System.out.println("limit = " + limit);
}
/**
* skip 跳过指定数量的元素
*/
@Test
void stream4Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
List<String> skip = strings.stream().skip(2).collect(Collectors.toList());
System.out.println("skip = " + skip);
}
/**
* map 操作集合中所有元素
*/
@Test
void stream5Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
List<String> concatHxlc = strings.stream().map(str -> str.concat("_hxlc")).collect(Collectors.toList());
List<String> hxlcConcat = strings.stream().map(str -> "hxlc" + str).collect(Collectors.toList());
strings.forEach(s -> System.out.println(s));
System.out.println("concatHxlc = " + concatHxlc);
System.out.println("hxlcConcat = " + hxlcConcat);
}
/**
* sort 对元素进行正序或倒序排序
*/
@Test
void stream6Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
//汉字正序
List<String> sort = strings.stream().sorted(Collator.getInstance(Locale.CHINA)).collect(Collectors.toList());
List<String> sort2 = strings.stream().distinct().sorted(Collator.getInstance(Locale.CHINA)).collect(Collectors.toList());
//按照指定字段进行排序
strings.stream().sorted((o1, o2) -> o1.getStationId().compareTo(o2.getStationId())).collect(Collectors.toList());
System.out.println("sort = " + sort);
System.out.println("sort2 = " + sort2);
//汉字倒序
List<String> sort1 = strings.stream().sorted(Collections.reverseOrder(Collator.getInstance(Locale.CHINA))).collect(Collectors.toList());
System.out.println("sort1 = " + sort1);
List<Integer> integers = Arrays.asList(9, 8, 5, -3, -5, 2);
List<Integer> sorted = integers.stream().sorted().collect(Collectors.toList());
System.out.println("sorted = " + sorted);
}
/**
* 集合中是否有一个元素满足条件
*/
@Test
void stream7Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
boolean anyMatch = strings.stream().anyMatch(s -> s.contains("九"));
System.out.println("anyMatch = " + anyMatch);
}
/**
* 集合中元素是否满足条件
*/
@Test
void stream8Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
boolean allMatch = strings.stream().allMatch(s -> s.length() > 1);
System.out.println("allMatch = " + allMatch);
}
/**
* 集合中所有元素都不满足条件
*/
@Test
void stream9Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
boolean b = strings.stream().noneMatch(s -> s.length() > 3);
System.out.println("b = " + b);
}
/**
* 返回集合中任意元素
* stream 串行流
* parallelStream 并行流
*/
@Test
void stream10Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
Optional<String> any = strings.stream().findAny();
if (any.isPresent())
System.out.println("any = " + any.get());
for (int i = 0; i < 20; i++) {
Optional<String> any1 = strings.parallelStream().findAny();
if (any.isPresent())
System.out.println("any1 = " + any1.get());
}
}
/**
* 返回第一个元素
*/
@Test
void stream11Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
for (int i = 0; i < 20; i++) {
Optional<String> first = strings.parallelStream().findFirst();
if (first.isPresent()) {
System.out.println("first = " + first.get());
}
}
}
/**
* 循环遍历
*/
@Test
void stream12Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
strings.stream().forEach(s -> System.out.println(s));
}
/**
* 将流转换成其它形式 list set map
*/
@Test
void stream13Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
Set<String> stringSet = strings.stream().collect(Collectors.toSet());
System.out.println("stringSet = " + stringSet);
Map<String, String> stringMap = strings.stream().collect(Collectors.toMap(k -> k, v -> "hxlc_" + v, (oldV, newV) -> newV));
System.out.println("stringMap = " + stringMap);
}
/**
* 将流中元素反复结合起来得到一个新的结果
*/
@Test
void stream14Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
Optional<String> reduce = strings.stream().reduce((a, b) -> {
return (a + b).replace("张", "zhang");
});
if (reduce.isPresent())
System.out.println("reduce.get() = " + reduce.get());
}
/**
* 获取集合中元素数量及最大最小值
*/
@Test
void stream15Test() {
List<String> strings = Arrays.asList("张三", "李四", "王五", "赵六", "张三", "吴八");
List<String> stringsNum = Arrays.asList("7", "8", "5", "3", "9", "10");
long count = strings.stream().count();
List<Integer> integers = Arrays.asList(7, 6, 8, 10, 9);
Integer maxInteger = integers.stream().max(Integer::compareTo).get();
Integer minInteger = integers.stream().min(Integer::compareTo).get();
System.out.println("maxInteger = " + maxInteger);
System.out.println("minInteger = " + minInteger);
System.out.println("count = " + count);
}
/**
* 类型转换 List<Map<String, Object>> -> List<BlackProductVo>
*/
@Test
void test() {
List<Map<String, Object>> list = null;
List<BlackProductVo> blackProductVoList = list.stream().map((item) -> {
BlackProductVo blackProductVo = new BlackProductVo();
blackProductVo.setIiiii(String.valueOf(item.get("IIIII")));
blackProductVo.setStartDate(String.valueOf(item.get("YYYYMMDD")));
blackProductVo.setEndDate(String.valueOf(item.get("YYYYMMDD")));
return blackProductVo;
}).collect(Collectors.toList());
}
/**
* 类型转换 List<BlackProductVo> -> Map<String, List<BlackProductVo>>
*/
@Test
void test1(){
Map<String, List<BlackProductVo>> bpMap = null;
List<BlackProductVo> blackProductVoList = null;
bpMap = blackProductVoList.stream().collect(Collectors.toMap(BlackProductVo::getIiiii, b -> {
List<BlackProductVo> list1 = new ArrayList<>();
list1.add(b);
return list1;
}, (v1, v2) -> {
v1.addAll(v2);
return v1;
}));
}