Stream流的使用及Lambda表达式与QueryWrapper的配合使用

实体类

@Data
public class WqWorkplace implements Serializable {
    /**
     * id
     */
    private Long id;
    /**
     * 职场
     */
    private String workplace;
    /**
     * 备注
     */
    private String remarks;
    /**
     * 时间
     */
    private String createTime;
}

一、Stream

去重

根据集合对象中的某个元素进行去重

  • 写法一(去重后为String集合)
List<WqWorkplace> workplaces=new ArrayList<>();
List<String> strings= workplaces.stream().map(WqWorkplace::getWorkplace).distinct().collect(Collectors.toList());        
  • 写法二(去重后为Object集合)
List<WqWorkplace> workplaces=new ArrayList<>();
List<WqWorkplace> wqWorkplaceList = workplaces.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(WqWorkplace::getWorkplace))), ArrayList::new));
  • 写法三(set接收)
List<WqWorkplace> stringList=new ArrayList<>();
Set<String> set=stringList.stream().map(f->(f.getWorkplace())).collect(Collectors.toSet());

根据集合对象中的多个元素进行去重

  • 写法一
List<WqWorkplace> workplaces=new ArrayList<>();
List<WqWorkplace> wqWorkplaceList = workplaces.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getWorkplace() + "-" + f.getRemarks()))), ArrayList::new));

循环

  • 写法一(循环list集合)
List<WqWorkplace> workplaces=new ArrayList<>();
        List<String> strings=new ArrayList<>();
        workplaces.stream().forEach(f->{
            //条件判断
            if (StringUtils.isNotBlank(f.getRemarks())){
                strings.add(f.getWorkplace());
            }
        });
  • 写法二(循环map集合)
List<String> stringList=new ArrayList<>();
Map<String, WqWorkplace> wqWorkplaceHashMap = new HashMap<>();
wqWorkplaceHashMap.put("key",new WqWorkplace());
wqWorkplaceHashMap.forEach((name, wqWorkplace) -> {
        String orginalName = wqWorkplace.getWorkplace();
        stringList.add(orginalName);
});

分页

  • 写法一(对list集合进行分页为map集合)
List<WqWorkplace> stringList=new ArrayList<>();
Map<String,Object> map = Maps.newHashMap();
//list -> 当前页所有数据  skip页码 limit条数
int pageNo=1;
int pageSize=10;
map.put("list",stringList.stream().skip((long) (pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList()));
//count -> 记录总条数
map.put("count",stringList.size());

二、 Lambda与QueryWrapper

前提条件使用的是mybatis-plus,引入service层

@Autowired
private WqWorkplaceService wqStationService;

查询

  • list()方法条件查询
List<WqWorkplace> wqWorkplace =wqStationService.list(new QueryWrapper<WqWorkplace>().lambda()
        .nested(f -> f.eq(WqWorkplace::getWorkplace,"查询的值")
                .or().like(WqWorkplace::getWorkplace,"查询的值")
        )
        .apply((ObjectUtil.isNotNull(wqWorkplace.getCreateTime())), "DATE_FORMAT(create_time, '%Y-%m-%d') >= {0}", "查询的值")
        .apply((ObjectUtil.isNotNull(wqWorkplace.getCreateTime())), "DATE_FORMAT(create_time, '%Y-%m-%d') <= {0}", "查询的值")
        .isNotNull(WqWorkplace::getWorkplace)
        .in(WqWorkplace::getId, Arrays.asList(2)));
  • getOne()方法查询单条数据
WqWorkplace wqWorkplace =wqStationService.getOne(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getWorkplace,"查询的值" ).last("LIMIT 1"));
  • page()方法分页查询
//页码
int pageIndex=1;
//页数
int pageSize=10;
Page<WqWorkplace> wqWorkplacePage = wqStationService.page(new Page<>(pageIndex , pageSize), new QueryWrapper<WqWorkplace>().lambda()
        .eq( WqWorkplace::getWorkplace, "查询的值")
);
  • getById()方法根据id查询
WqWorkplace wqWorkplaces = wqStationService.getById(1);
  • count()方法条数查询
int count = wqStationService.count(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getWorkplace,"查询的值"));

修改

  • update()方法根据条件修改
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqWorkplace.setWorkplace("沙发沙发");
wqStationService.update(wqWorkplace,new UpdateWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getId,wqWorkplace.getId()).eq(WqWorkplace::getWorkplace,wqWorkplace.getWorkplace()));

  • updateById()方法根据数据库主键id修改
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqWorkplace.setWorkplace("岗位1");
wqStationService.updateById(wqWorkplace);

删除

  • remove()方法根据条件删除
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqStationService.remove(new QueryWrapper<WqWorkplace>().lambda().eq(WqWorkplace::getId,wqWorkplace.getId()));
  • removeById()方法根据数据库主键id删除
WqWorkplace wqWorkplace=new WqWorkplace();
wqWorkplace.setId(new Long(1));
wqStationService.removeById(wqWorkplace);
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值