您当前的位置: 首页 > 

杨林伟

暂无认证

  • 3浏览

    0关注

    3337博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Guava Lists工具类

杨林伟 发布时间:2022-01-25 11:32:40 ,浏览量:3

文章目录
  • 01 概述
  • 02 Lists工具类
  • 03 文末

01 概述

GuavaGoogle 开源的一个 Java 工具库,里面有很多工具类,本文要讲的是里面的Lists工具类。

注意,使用Guava工具类库,必须先添加依赖:


    com.google.guava
    guava
    28.0

02 Lists工具类

从下图可以看到Lists工具类有很多种方法: 在这里插入图片描述 下面举几个比较典型的操作演示下:

① 使用一行代码就能创建集合了,如下:

  ArrayList students = Lists.newArrayList(
                new Student("张三", 33),
                new Student("李四", 40),
                new Student("王五", 23),
                new Student("赵六", 55));

② 集合间转换(Sets也是仿照Lists的),如下:

HashSet strHashSet = Sets.newHashSet("1", "2", "3");
ArrayList strList = Lists.newArrayList(strHashSet);

③ 集合分页(虽然stream流也能做到,也看下吧),如下:

ArrayList students = Lists.newArrayList(
               new Student("张三", 33),
               new Student("李四", 40),
               new Student("王五", 23),
               new Student("赵六", 55));

// 每页两条
List partionStudent = Lists.partition(students, 2);

④ dto转vo时用到,如下:

List studentVoList = Lists.transform(studentList, new Function() {
    @Override
    public StudentVo apply(Student student) {
        StudentVo s = new StudentVo();
        try {
            BeanUtils.copyProperties(s, student);
        } catch (Exception e) {
        }
        s.setStudent_id(student.getId());
        s.setStudent_no(student.getStuNo());
        return s;
    }
});
03 文末

本文就举Lists用法的几个典型场景例子,不过这工具类也是有不完善之处的(例如:transform方法),各位童鞋按需使用吧。

关注
打赏
1662376985
查看更多评论
立即登录/注册

微信扫码登录

0.0580s