java8 stream 常用转换方法

Set → List

java 复制代码
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));

// 转为 ArrayList
List<String> list = new ArrayList<>(set);

// 转为 LinkedList(如果需要)
List<String> linkedList = new LinkedList<>(set);

Set → Array

java 复制代码
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));

// 方法1:传入空数组(推荐,简洁)
String[] array = set.toArray(new String[0]);

// 方法2:传入指定大小的数组(兼容旧代码)
String[] array2 = set.toArray(new String[set.size()]);

上面2个有也有stream

java 复制代码
// Set → List
List<String> list = set.stream().collect(Collectors.toList());

// Set → Array
String[] array = set.stream().toArray(String[]::new);

List => map

java 复制代码
@Data
@Table(name = "sys_dict_data")
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public class DictData extends BaseEntity {
    long dictCode;
    String dictLabel;
    String dictValue;
    String dictType;
    long dictSort;
    String listClass;
    String isDefault;
    String remark;
}
java 复制代码
 // 转换为 Map
      Map<String, DictData> dictMap = dictList.stream()
    .collect(Collectors.toMap(
        DictData::getDictValue, // 作为key
        Function.identity(),
        (v1, v2) -> v1,
        LinkedHashMap::new  // 保持插入顺序
    ));

Stream 基本语法

java 复制代码
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();

List stream 转换 + 过滤 List => List

java 复制代码
private DictDataVO convertToVO(DictData dictData) {
        DictDataVO vo = new DictDataVO();
        BeanUtils.copyProperties(dictData, vo);

        if (dictData.getStatus() != null && !dictData.getStatus().trim().isEmpty()) {
            String statusName = dictCacheService.getDictLabel("sys_normal_disable", dictData.getStatus().trim());
            vo.setStatusName(statusName);
        } else {
            vo.setStatusName("");
        }

        return vo;
    }
java 复制代码
  // 转换为 VO 并填充字典名称 实体类转为前端接受的类
        List<UserVO> voList = userList.stream()
                .filter(Objects::nonNull)
                .map(this::convertToVO)
                .collect(Collectors.toList());

arrays stream 转换 + 过滤

参数是数组, 去除前后空格,删除空的值, 转为long类型, 最后转成List

java 复制代码
  List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .map(Long::parseLong)
                .collect(Collectors.toList());

求和

java 复制代码
List<Integer> nums = Arrays.asList(10, 20, 30);
int sum = nums.stream().mapToInt(Integer::intValue).sum();
// 或直接:
int sum2 = nums.stream().reduce(0, Integer::sum);

去重 + 排序

java 复制代码
List<String> words = Arrays.asList("banana", "apple", "banana", "cherry");
List<String> uniqueSorted = words.stream()
                                 .distinct()
                                 .sorted()
                                 .collect(Collectors.toList());
// 结果: ["apple", "banana", "cherry"]
相关推荐
懒鸟一枚4 小时前
深入理解 Linux 内存、Swap 交换分区与分页机制的关系
java·linux·数据库
我命由我123455 小时前
执行 Gradle 指令报错,无法将“grep”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
考虑考虑5 小时前
Sentinel安装
java·后端·微服务
凤凰院凶涛QAQ7 小时前
《Java版数据结构 & 集合类剖析》栈与队列:“push/pop 是栈的灵魂,offer/poll 是队列的骨架——四组 API,两种人生”
java·开发语言·数据结构
掘金_答案8 小时前
上线那天,一个 ConcurrentHashMap 差点送走我的 AI 客服——3 天排查 JVM 血泪史
java·后端·架构
猿与禅8 小时前
CosId 分布式 ID 生成器完全教程:从架构原理到生产落地
java·shardingsphere·雪花算法·分布式id·高性能·cosid·号段模式
MindUp8 小时前
企业网盘权限模型解析:多层级访问控制与审计能力选型指南
java·linux·运维
NG4778 小时前
【软件设计与体系结构】-策略设计模式
java·设计模式·软件工程
程序员天天困9 小时前
优雅记录操作日志:从注解到 SpEL 的全链路实践与开源方案对比
java·后端
战血石LoveYY9 小时前
Integer类型超限,导致数据异常
java·算法