java元帅

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  49 随笔 :: 0 文章 :: 0 评论 :: 19902 阅读
package com.test;

import com.test.dao.Person;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestApplication {
static List<Person> personList = new ArrayList<Person>();
private static void initPerson() {
personList.add(new Person("张三", 8, 3000));
personList.add(new Person("李四", 18, 5000));
personList.add(new Person("王五", 28, 7000));
personList.add(new Person("孙六", 38, 9000));
}
public static void main(String[] args) {
List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

// 遍历输出符合条件的元素
list.stream().filter(x -> x > 6).forEach(System.out::println);
// 匹配第一个
Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
// 匹配任意(适用于并行流)
Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
// 是否包含符合特定条件的元素
boolean anyMatch = list.stream().anyMatch(x -> x < 6);
System.out.println("匹配第一个值:" + findFirst.get());
System.out.println("匹配任意一个值:" + findAny.get());
System.out.println("是否存在大于6的值:" + anyMatch);
filter01();
test02();
test05();
test06();
test07();
test08();
test09();
test10();
test11();
test13();
test14();
// test15(); 有报错
test16();
}

/**
* 筛选员工中未满18周岁的人,并形成新的集合
* @思路
* List<Person> list = new ArrayList<Person>();
* for(Person person : personList) {
* if(person.getAge() > 18) {
* list.add(person);
* }
* }
*/
private static void filter01(){
initPerson();
//(1)筛选员工中未满18周岁的人,并形成新的集合
List<Person> collect = personList.stream().filter(x -> x.getAge()> 18).collect(Collectors.toList());
System.out.println("filter01:"+collect);

}

/**
* 获取String集合中最长的元素
* @思路
* List<String> list = Arrays.asList("zhangsan", "lisi", "wangwu", "sunliu");
* String max = "";
* int length = 0;
* int tempLength = 0;
* for(String str : list) {
* tempLength = str.length();
* if(tempLength > length) {
* length = str.length();
* max = str;
* }
* }
* @return zhangsan
*/
private static void test02(){
List<String> list = Arrays.asList("zhangsan", "lisi", "wangwu", "sunliu");
//比较传入条件是比较字符串的长度
Comparator<? super String> comparator = Comparator.comparing(String::length);
Optional<String> max = list.stream().min(comparator);
System.out.println(max);
}
//(2)获取Integer集合中的最大值
private static void test05() {
List<Integer> list = Arrays.asList(1,4,7,34,23,67,54);
Optional<Integer> max = list.stream().max(Integer::compareTo);
//自定义排序
Optional<Integer> max1 = list.stream().min(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println(max1);

}
//获取员工中年龄最大的人
private static void test06() {
initPerson();
Comparator<Person> anInt = Comparator.comparingInt(Person::getAge);
Optional<Person> max = personList.stream().max(anInt);
System.out.println(max);

}
//(4)计算integer集合中大于10的元素的个数
private static void test07() {
List<Integer> list = Arrays.asList(1, 23, 32, 2, 54, 14);
long count = list.stream().filter(x -> x > 10).count();
Optional<Integer> max = list.stream().filter(x -> x > 10).max(Integer::compareTo);
System.out.println("count:"+count+"max:"+max);
}
//map和flatMap
private static void test08() {
List<String> list = Arrays.asList("zhangsan", "lisi", "wangwu", "sunliu");
// list.stream().forEach(x -> x.toUpperCase());
//(1)字符串大写
List<String> collect = list.stream().map(x -> x.toUpperCase()).collect(Collectors.toList());
List<String> collect1 = list.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collect);
System.out.println(collect1);
}
//(2)整数数组每个元素+3
/**
* 整数数组每个元素+3
* @思路
* List<Integer> list = Arrays.asList(1, 17, 27, 7);
List<Integer> list2 = new ArrayList<Integer>();
for(Integer num : list) {
list2.add(num + 3);
}
@return [4, 20, 30, 10]
*/
private static void test09() {
List<Integer> list = Arrays.asList(1, 17, 27, 7);
List<Integer> collect = list.stream().map(x -> x + 3).collect(Collectors.toList());
System.out.println(collect);
}
//公司效益好每人涨两千
private static void test10() {
initPerson();
List<Object> collect = personList.stream().map(x -> {
x.setSalary(x.getSalary() + 2000);
return x;
}).collect(Collectors.toList());
System.out.println(collect);
}

//(4)将两个字符数组合并成一个新的字符数组
private static void test11() {
String[] arr = {"z, h, a, n, g", "s, a, n"};
List<String> list = Arrays.asList(arr);
System.out.println(list);
List<String> collect = list.stream().flatMap(x -> {
String[] array = x.split(",");
Stream<String> stream = Arrays.stream(array);
return stream;
}).collect(Collectors.toList());
System.out.println(collect);
}


//(1)求Integer集合的元素之和、乘积和最大值
private static void test13() {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Optional<Integer> reduce = list.stream().reduce((x, y) -> x + y);
System.out.println("求和:"+reduce);
//求积
Optional<Integer> reduce2 = list.stream().reduce((x,y) -> x * y);
System.out.println("求积:"+reduce2);
//求最大值
Optional<Integer> reduce3 = list.stream().reduce((x,y) -> x>y?x:y);
System.out.println("求最大值:"+reduce3);
}

//(2)求所有员工的工资之和和最高工资
private static void test14() {
initPerson();
Optional<Integer> reduce = personList.stream().map(Person::getSalary).reduce(Integer::sum);
Optional<Integer> reduce2 = personList.stream().map(Person :: getSalary).reduce(Integer::max);
System.out.println("工资之和:"+reduce);
System.out.println("最高工资:"+reduce2);
}

//6、收集(toList、toSet、toMap)
//取出大于18岁的员工转为map
private static void test15(){
initPerson();
Map<String, Person> collect1 = personList.stream().filter(x -> x.getAge() > 18).collect(Collectors.toMap(Person::getName, y -> y));
System.out.println(collect1);

}

/*7、collect
Collectors提供了一系列用于数据统计的静态方法:
计数: count
平均值: averagingInt、 averagingLong、 averagingDouble
最值: maxBy、 minBy
求和: summingInt、 summingLong、 summingDouble
统计以上所有: summarizingInt、 summarizingLong、 summarizingDouble
*/
private static void test16(){
initPerson();
//统计员工人数
Long collect = personList.stream().collect(Collectors.counting());
System.out.println("统计员工人数:"+collect);
//求平均工资
Double collect1 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
System.out.println("求平均工资:"+collect);
//求最高工资
Optional<Integer> collect2 = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compareTo));
System.out.println("求最高工资:"+collect2);
//求工资之和
Integer collect3 = personList.stream().collect(Collectors.summingInt(Person::getSalary));
System.out.println("求最高工资:"+collect3);
//一次性统计所有信息
DoubleSummaryStatistics collect4 = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
System.out.println("一次性统计所有信息:"+collect4);
}
/**
* 8、分组(partitioningBy/groupingBy)
* 分区:将stream按条件分为两个 Map,比如员工按薪资是否高于8000分为两部分。
*
* 分组:将集合分为多个Map,比如员工按性别分组。有单级分组和多级分组。
*/


}
posted on   java元帅  阅读(97)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示