Java 代码和使用steam流(List对象使用流操作示例,Java正则匹配,获取当前操作系统)

2 篇文章 0 订阅
1 篇文章 0 订阅

list<t> 转list<c>

List<UUID> cdids = deviceId.stream().map(s -> UUID.fromString(s.trim())).collect(Collectors.toList());

list对象分组

Map<String,List<ElecMonthStatementTemplate>> map = elecMonthStatementTemplates.stream().collect(Collectors.groupingBy(ElecMonthStatementTemplate::getElecTableName));

object转对象

ObjectMapper objectMapper = new ObjectMapper();

UserModel userModel = objectMapper.convertValue(claims, UserModel.class);

遍历list替换指定的值

  • for (DropofpressureVo list : s) {
  • DropofpressureVo vo=new DropofpressureVo();
  • List collect = list.getList().stream().map(str -> str.contains("-") ? "-" : str).collect(Collectors.toList());
  • vo.setList(collect);
  • vo.setTime(list.getTime());
  • vo.setName(list.getName());
  • w.add(vo);
  • }

Java  使用正则匹配

  • public static void main(String[] args) {
  • String str = "10.0*0.067+247.0*0.1283+(1.24+1.2+1.2+0+0+19+3013.2+0.25+0.45+0.0005*1000+0.32+704.6+0.35+490.66+0.5+0.4+0.49+0.55+0.305+0.3+0.22+0.26+0.31+0.26+0.15+0.1+3.6+6.0+0.3+85.44+0.35+0.31+0.62)*(862+249+(726+0+2296)*1/12)/(0+862+249+726+2296+17496+0)+(694.3+0.24+0.34)*0.0591+(0+9590+0+0+0+9396+0+1699.6+765+360.6+359+441.1+0.56+0.24+490.4+484.9+0.36+121.28)*0.1233+2.2+13.2+0+(0.36+121.28)*0.1522";
  • String regex = "\\(([^\\(\\)]+)\\)";
  • Pattern pattern = Pattern.compile(regex);
  • Matcher matcher = pattern.matcher(str);
  • List list = new ArrayList<>();
  • while (matcher.find()) {
  • list.add(matcher.group(1));
  • }
  • for (String val : list) {
  • System.out.println("val = " + val);
  • }
  • }

从list中查找对应的值

  • Optional cartOptional = zuodouTaskItems.stream().filter(item -> item.getId().equals(record.getItemsId())).findFirst();
  • if (cartOptional.isPresent()) {
  • // 存在
  • ZuodouTaskItem zuodouTaskItem = cartOptional.get();
  • record.setQty(new BigDecimal(zuodouTaskItem.getQty()));
  • record.setRQty(new BigDecimal(zuodouTaskItem.getRQty()));
  • record.setIId(zuodouTaskItem.getIId());
  • } else {
  • // 不存在
  • }

获取操作系统

System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1

List对象拿指定属性

List asIdList = zuodouTaskDatasArrayList.stream() .map(ZuodouTaskDatas::getAsId) // 使用map方法提取属性值.collect(Collectors.toList());

根据civerid分组统计

  • Map<String, List<ZuodouCourseManagement>> collect = zuodouCourseManagements.stream().collect(Collectors.groupingBy(ZuodouCourseManagement::getCourseCoverId));

我一个list对象需要根据 chapterName ,chapterOrder 两个属性分组 返回 [ {chapterName :第一 ,chapterOrder :1 , datas:[ List<ZuodouCourseManagementVo> ] } , {chapterName :第二 ,chapterOrder :2 , datas:[ List<ZuodouCourseManagementVo> ] } ]

  • List<Map<String, Object>> result = zuodouCourseManagementVos.stream()
    • .sorted(Comparator.comparing(ZuodouCourseManagementVo::getChapterOrder))
      • .collect(Collectors.groupingBy(ZuodouCourseManagementVo::getChapterName))
      • .entrySet().stream()
      • .map(entry -> {
        • Map<String, Object> group = new HashMap<>();
          • group.put("chapterName", entry.getKey());
          • group.put("chapterOrder", entry.getValue().get(0).getChapterOrder());
          • group.put("datas", entry.getValue());
        • return group;
  • }).collect(Collectors.toList());
  • 首先使用 sorted 方法对 zuodouCourseManagementVos 进行排序,按照 chapterOrder 属性进行升序排序。然后,使用 groupingBy 方法按照 chapterName 将列表分组。接着,使用 entrySet 方法获取分组后的 Map 的键值对集合,并使用 stream 进行处理。对于每个键值对,我们创建一个新的 Map 对象 group,并将 entry.getKey()(即 chapterName)作为键,entry.getValue().get(0).getChapterOrder()(即第一个元素的 chapterOrder)作为值。然后,将分组的数据列表 entry.getValue() 作为 datas 的值。最后,收集这些 Map 对象到一个列表中。这样,你将获得一个列表 result,其中每个元素都包含 chapterName、chapterOrder 和对应的数据列表。

根据指定属性返回匹配值

List<ZuodouCourseManagementVo> matchedElements = zuodouCourseManagementVos.stream() .filter(vo -> vo.getChapterName().equals(targetChapterName) && vo.getChapterOrder() == targetChapterOrder) .collect(Collectors.toList());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值