当前位置: 首页 > news >正文

java常用数据转换

1.   List与数组互转
    ArrayList<String> list = new ArrayList<>();
    String[] array = list.stream().toArray(String[]::new);


    String[] array1 = {"apple", "banana", "orange"};
    List<String> list1 = Arrays.stream(array1).collect(Collectors.toList());

    String[] cateArray = cateList.toArray(new String[cateList.size()]);

2.new集合对像
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    List<String> arrayList = Lists.newArrayList();
    Map<String, Object[]> concurrentMap = Maps.newConcurrentMap();
    Map<String, Object> resultMap = Maps.newHashMap();

3.集合转字符串

   例1:List<String> attachListFar = new ArrayList<>();
       attachListFar.add(attachDto.getFilePath());
    String attachListFarStr = String.join(",", attachListFar);

例2:if (StringUtils.isNotBlank(approvedBy)) {StringJoiner userStrJoin = new StringJoiner(",");String[] userIds = approvedBy.split(",");for (String uId : userIds) {String com = RedisUtil.getName(RedisConstant.SYS_DEPT_USER_ID, uId);userStrJoin.add(com);}outDto.setApprovedByName(userStrJoin.toString());//多人名字
}

    4.stream集合变换:
    //list对象
    List<String> idList= todoViewList.stream().map(HdInfoTodoView::getId).collect(Collectors.toList());
    dto.setHdIds(listWithoutRepeat.toArray(new String[listWithoutRepeat.size()]));
    Map<String, List<SafetyInspectRecordDetail>> map = list.stream().collect(Collectors.groupingBy(SafetyInspectRecordDetail::getInspectRecordId));
   //map对象
Map<String, String> levelMap = inspectLevelResult.getData() == null ? Maps.newConcurrentMap() : inspectLevelResult.getData().stream().collect(Collectors.toMap(SysDictDataOutDto::getDictValue, SysDictDataOutDto::getDictLabel, (k, v) -> v));
    
    //map对象
    Map<String, HdInfoOutDto> hdMap = list.stream().collect(Collectors.toMap(HdInfoOutDto::getId, p -> p, (k, v) -> v));
                
    // 去重
     List<String> userNameList = list.stream().filter(c -> MyStringUtil.isNotEmpty(c.getUserName())).map(CisAlarm::getUserName).distinct().collect(Collectors.toList());
     
    


http://www.mrgr.cn/news/95236.html

相关文章:

  • 蓝桥真题讲解
  • 【C#】CS学习之Modbus通讯
  • 24. 状态模式
  • Vulnhub-wordpress通关攻略
  • 蓝桥杯 之 暴力回溯
  • 切线、斜率、梯度和导数以及其关系
  • css-grid布局
  • 限幅滤波法对数据进行滤波优化
  • Vulnhub-dedecms织梦通关攻略
  • 【C++网络编程】第2篇:简单的TCP服务器与客户端
  • CIR-Net:用于 RGB-D 显著性目标检测的跨模态交互与优化(问题)
  • vmware下linux无法上网解决方法
  • 啃书—以国产化光耦ORPC-847芯片手册为例
  • 字节大模型面经
  • 单片机flash存储也做磨损均衡
  • 【C#语言】C#中的同步与异步编程:原理、示例与最佳实践
  • RAG各类方法python源码解读与实践:RAG技术综合评测【3万字长文】
  • Redis核心机制(一)
  • C++学习之nginx+fastDFS
  • 从零开始实现Stable Diffusion本地部署