java Stream操作 转换

流创建

方法
集合Collection.stream/parllelStream
数组Arrays.stream
数字StreamIntStream/LongStream.range/rangeClosed/Random.inis/longs/doubles
自己创建stream.generate/iterate

1、list 转 map

工作中,我们经常遇到listmap的案例。Collectors.toMap就可以把一个list数组转成一个Map。代码如下:

public class TestLambda {

    public static void main(String[] args) {

        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(new UserInfo(1L, "测试源码", 18));
        userInfoList.add(new UserInfo(2L, "程序员淘淘", 27));
        userInfoList.add(new UserInfo(2L, "打代码的淘淘", 26));

        /**
         *  list 转 map
         *  使用Collectors.toMap的时候,如果有可以重复会报错,所以需要加(k1, k2) -> k1
         *  (k1, k2) -> k1 表示,如果有重复的key,则保留第一个,舍弃第二个
         */
        Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUserId, userInfo -> userInfo, (k1, k2) -> k1));
        userInfoMap.values().forEach(a->System.out.println(a.getUserName()));
    }
}

类似的,还有Collectors.toList()Collectors.toSet(),表示把对应的流转化为list或者Set

2、filter()过滤

从数组集合中,过滤掉不符合条件的元素,留下符合条件的元素。

List<UserInfo> userInfoList = new ArrayList<>();
userInfoList.add(new UserInfo(1L, "测试源码", 18));
userInfoList.add(new UserInfo(2L, "程序员淘淘", 27));
userInfoList.add(new UserInfo(3L, "打代码的淘淘", 26));
        
/**
 * filter 过滤,留下超过18岁的用户
 */
List<UserInfo> userInfoResultList = userInfoList.stream().filter(user -> user.getAge() > 18).collect(Collectors.toList());
userInfoResultList.forEach(a -> System.out.println(a.getUserName()));
        
//运行结果
程序员淘淘
打代码的淘淘

3、foreach 遍历

foreach 遍历list,遍历map,真的很丝滑。

/**
 * forEach 遍历集合List列表
 */
List<String> userNameList = Arrays.asList("测试源码", "程序员淘淘", "艿艿");
userNameList.forEach(System.out::println);
 
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("号码你", "测试源码");
hashMap.put("职业", "程序员淘淘");
hashMap.put("昵称", "艿艿");
/**
 *  forEach 遍历集合Map
 */
hashMap.forEach((k, v) -> System.out.println(k + ":\t" + v));

//运行结果
测试源码
程序员淘淘
打代码的淘淘
职业: 程序员淘淘
号码你: 测试源码
昵称: 艿艿

4、groupingBy 分组

提到分组,相信大家都会想起SQLgroup by。我们经常需要一个List做分组操作。比如,按城市分组用户。在Java8之前,是这么实现的:

List<UserInfo> originUserInfoList = new ArrayList<>();
originUserInfoList.add(new UserInfo(1L, "测试源码", 18,"深圳"));

originUserInfoList.add(new UserInfo(3L, "打代码的淘淘", 26,"湛江"));
originUserInfoList.add(new UserInfo(2L, "程序员淘淘", 27,"深圳"));
Map<String, List<UserInfo>> result = new HashMap<>();

for (UserInfo userInfo : originUserInfoList) {
  String city = userInfo.getCity();
  List<UserInfo> userInfos = result.get(city);
  if (userInfos == null) {
      userInfos = new ArrayList<>();
      result.put(city, userInfos);
    }
  userInfos.add(userInfo);
}

而使用Java8groupingBy分组器,清爽无比:

Map<String, List<UserInfo>> result = originUserInfoList.stream()
.collect(Collectors.groupingBy(UserInfo::getCity));

5、sorted+Comparator 排序

工作中,排序的需求比较多,使用sorted+Comparator排序,真的很香。

List<UserInfo> userInfoList = new ArrayList<>();
userInfoList.add(new UserInfo(1L, "测试源码", 18));
userInfoList.add(new UserInfo(3L, "打代码的淘淘", 26));
userInfoList.add(new UserInfo(2L, "程序员淘淘", 27));

/**
 *  sorted + Comparator.comparing 排序列表,
 */
userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge)).collect(Collectors.toList());
userInfoList.forEach(a -> System.out.println(a.toString()));

System.out.println("开始降序排序");

/**
 * 如果想降序排序,则可以使用加reversed()
 */
userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).reversed()).collect(Collectors.toList());
userInfoList.forEach(a -> System.out.println(a.toString()));

//运行结果
UserInfo{userId=1, userName='测试源码', age=18}
UserInfo{userId=3, userName='打代码的淘淘', age=26}
UserInfo{userId=2, userName='程序员淘淘', age=27}
开始降序排序
UserInfo{userId=2, userName='程序员淘淘', age=27}
UserInfo{userId=3, userName='打代码的淘淘', age=26}
UserInfo{userId=1, userName='测试源码', age=18}


-=-----------------------------------------------------------------
private static void test04(){
    // 按工资升序排序(自然排序)
    List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName)
            .collect(Collectors.toList());
    // 按工资倒序排序
    List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed())
            .map(Person::getName).collect(Collectors.toList());
    // 先按工资再按年龄升序排序
    List<String> newList3 = personList.stream()
            .sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName)
            .collect(Collectors.toList());
    // 先按工资再按年龄自定义排序(降序)
    List<String> newList4 = personList.stream().sorted((p1, p2) -> {
        if (p1.getSalary() == p2.getSalary()) {
            return p2.getAge() - p1.getAge();
        } else {
            return p2.getSalary() - p1.getSalary();
        }
    }).map(Person::getName).collect(Collectors.toList());
 
    System.out.println("按工资升序排序:" + newList);
    System.out.println("按工资降序排序:" + newList2);
    System.out.println("先按工资再按年龄升序排序:" + newList3);
    System.out.println("先按工资再按年龄自定义降序排序:" + newList4);
}

6、distinct去重

distinct可以去除重复的元素:

List<String> list = Arrays.asList("A", "B", "F", "A", "C");
List<String> temp = list.stream().distinct().collect(Collectors.toList());
temp.forEach(System.out::println);

7、findFirst 返回第一个

findFirst 很多业务场景,我们只需要返回集合的第一个元素即可:

List<String> list = Arrays.asList("A", "B", "F", "A", "C");
list.stream().findFirst().ifPresent(System.out::println);

8、anyMatch 是否至少匹配一个元素

anyMatch 检查流是否包含至少一个满足给定谓词的元素。

Stream<String> stream = Stream.of("A", "B", "C", "D");
boolean match = stream.anyMatch(s -> s.contains("C"));
System.out.println(match);
//输出
true

9、allMatch 匹配所有元素

allMatch 检查流是否所有都满足给定谓词的元素。

Stream<String> stream = Stream.of("A", "B", "C", "D");
boolean match = stream.allMatch(s -> s.contains("C"));
System.out.println(match);
//输出
false

10、map 转换

map方法可以帮我们做元素转换,比如一个元素所有字母转化为大写,又或者把获取一个元素对象的某个属性,demo如下:

List<String> list = Arrays.asList("jay", "tianluo");
//转化为大写
List<String> upperCaselist = list.stream().map(String::toUpperCase).collect(Collectors.toList());
upperCaselist.forEach(System.out::println);

11、Reduce

Reduce可以合并流的元素,并生成一个值

int sum = Stream.of(1, 2, 3, 4).reduce(0, (a, b) -> a + b);
System.out.println(sum);


/**
 * 求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);
}


/*
 * 求所有员工的工资之和和最高工资
 */
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);
}

12、peek 打印个日志

peek()方法是一个中间Stream操作,有时候我们可以使用peek来打印日志。

List<String> result = Stream.of("程序员淘淘", "测试源码", "打代码的淘淘")
            .filter(a -> a.contains("芋艿"))
            .peek(a -> System.out.println("关注号码你:" + a)).collect(Collectors.toList());
System.out.println(result);
//运行结果
关注号码你:程序员淘淘
关注号码你:测试源码
[程序员淘淘, 测试源码]

13、Max,Min 最大最小

使用lambda流求最大,最小值,非常方便。

List<UserInfo> userInfoList = new ArrayList<>();
userInfoList.add(new UserInfo(1L, "测试源码", 18));
userInfoList.add(new UserInfo(3L, "打代码的淘淘", 26));
userInfoList.add(new UserInfo(2L, "程序员淘淘", 27));

Optional<UserInfo> maxAgeUserInfoOpt = userInfoList.stream().max(Comparator.comparing(UserInfo::getAge));
maxAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("max age user:" + userInfo));

Optional<UserInfo> minAgeUserInfoOpt = userInfoList.stream().min(Comparator.comparing(UserInfo::getAge));
minAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("min age user:" + userInfo));

//运行结果
max age user:UserInfo{userId=2, userName='程序员淘淘', age=27}
min age user:UserInfo{userId=1, userName='测试源码', age=18}

14、count 统计

一般count()表示获取流数据元素总数。

List<UserInfo> userInfoList = new ArrayList<>();
userInfoList.add(new UserInfo(1L, "测试源码", 18));
userInfoList.add(new UserInfo(3L, "打代码的淘淘", 26));
userInfoList.add(new UserInfo(2L, "程序员淘淘", 27));

long count = userInfoList.stream().filter(user -> user.getAge() > 18).count();
System.out.println("大于18岁的用户:" + count);
//输出
大于18岁的用户:2
    
    
    
    
/**
 * 统计员工人数、平均工资、工资总额、最高工资
 */
private static void test01(){
    //统计员工人数
    Long count = personList.stream().collect(Collectors.counting());
    //求平均工资
    Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
    //求最高工资
    Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
    //求工资之和
    Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary));
    //一次性统计所有信息
    DoubleSummaryStatistics collect = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
    System.out.println("统计员工人数:"+count);
    System.out.println("求平均工资:"+average);
    System.out.println("求最高工资:"+max);
    System.out.println("求工资之和:"+sum);
    System.out.println("一次性统计所有信息:"+collect);
}

15、提取/组合

流也可以进行合并、去重、限制、跳过等操作。

private static void test05(){
    String[] arr1 = { "a", "b", "c", "d" };
    String[] arr2 = { "d", "e", "f", "g" };
    Stream<String> stream1 = Stream.of(arr1);
    Stream<String> stream2 = Stream.of(arr2);
    // concat:合并两个流 distinct:去重
    List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
    // limit:限制从流中获得前n个数据
    List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
    // skip:跳过前n个数据
    List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());
 
    System.out.println("流合并:" + newList);
    System.out.println("limit:" + collect);
    System.out.println("skip:" + collect2);
}

16、连接joining

joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。

List<Integer> list = Arrays.asList(7, 6, 9, 3, 10, 2, 1);
String collect = list.stream().map(String::valueOf).collect(Collectors.joining(","));
System.out.println(collect);

7,6,9,3,10,2,1

17、常用函数式接口

其实lambda离不开函数式接口,我们来看下JDK8常用的几个函数式接口:

  • Function<T, R>(转换型): 接受一个输入参数,返回一个结果
  • Consumer<T> (消费型): 接收一个输入参数,并且无返回操作
  • Predicate<T> (判断型): 接收一个输入参数,并且返回布尔值结果
  • Supplier<T> (供给型): 无参数,返回结果

Function<T, R> 是一个功能转换型的接口,可以把将一种类型的数据转化为另外一种类型的数据

    private void testFunction() {
        //获取每个字符串的长度,并且返回
        Function<String, Integer> function = String::length;
        Stream<String> stream = Stream.of("程序员淘淘", "测试源码", "打代码的淘淘");
        Stream<Integer> resultStream = stream.map(function);
        resultStream.forEach(System.out::println);
    }

Consumer<T>是一个消费性接口,通过传入参数,并且无返回的操作

   private void testComsumer() {
        //获取每个字符串的长度,并且返回
        Consumer<String> comsumer = System.out::println;
        Stream<String> stream = Stream.of("程序员淘淘", "测试源码", "打代码的淘淘");
        stream.forEach(comsumer);
    }

Predicate<T>是一个判断型接口,并且返回布尔值结果.

    private void testPredicate() {
        //获取每个字符串的长度,并且返回
        Predicate<Integer> predicate = a -> a > 18;
        UserInfo userInfo = new UserInfo(2L, "程序员淘淘", 27);
        System.out.println(predicate.test(userInfo.getAge()));
    }

Supplier<T>是一个供给型接口,无参数,有返回结果。

    private void testSupplier() {
        Supplier<Integer> supplier = () -> Integer.valueOf("666");
        System.out.println(supplier.get());
    }

这几个函数在日常开发中,也是可以灵活应用的,比如我们DAO操作完数据库,是会有个result的整型结果返回。我们就可以用Supplier<T>来统一判断是否操作成功。如下:

    private void saveDb(Supplier<Integer> supplier) {
        if (supplier.get() > 0) {
        System.out.println("插入数据库成功");
        }else{
        System.out.println("插入数据库失败");
        }
        }

@Test
public void add() throws Exception {
        Course course=new Course();
        course.setCname("java");
        course.setUserId(100L);
        course.setCstatus("Normal");
        saveDb(() -> courseMapper.insert(course));
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TyRed08

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值