当前位置: 首页 > 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());
     
    

相关文章:

  • 软件设计师笔记持续更新-看学以致知视频笔记
  • 中微单片机学习--SC8F072
  • 【蓝桥杯速成】| 8.回溯算法
  • C语言 第五章 指针(3)
  • 实用工具-Another Redis Desktop Manager介绍
  • electron桌面应用多种快速创建方法
  • 如何通过Odoo 18创建与配置服务器操作
  • 楼宇自控系统的结构密码:总线与分布式结构方式的差异与应用
  • Jackson 库进行 JSON 序列化时遇到了 ‌无限递归(Infinite Recursion)‌ 问题
  • 嵌入式笔记 | 正点原子STM32F103ZET6 5 | 串口通信
  • kafka的文章
  • C#从入门到精通(1)
  • 路由Vue Router基本用法
  • QEMU 中 x86_cpu_realizefn 到 ept_emulation_fault 的调用流程解析(macos)
  • 数据库的两种模式
  • 国内首家,百度智能云千帆AppBuilder全面兼容MCP协议
  • dfs(二十一)46. 全排列 中等
  • 【Linux】信号:产生信号
  • 夜莺监控 v8.0 新版通知规则 | 对接飞书告警
  • 【数据分析】数据筛选与访问行列元素3
  • 伙购网官方网站/全国疫情最新报告
  • 网站辅助色怎么用/nba最新新闻
  • 怎样查看别人的网站是怎么建设/seo流量
  • 东营做网站优化的公司/武汉大学人民医院官网
  • 网站建设技术员/html网页制作代码大全
  • 网站首页的动态效果图怎么做/seo优化必备技巧