小工具知识网

当前位置: 首页 > 行业知识 > Java:List转换List

行业知识

Java:List转换List

2026-12-07 04:54:34 来源:互联网转载

我们经常会遇到泛型转换泛型的时候,今天我们就介绍下使用java1.8和普通转换,其中测试数据自行添加

()
@Datapublic class Study1{    private  String no;      // 学号    private  String name; // 姓名}@Datapublic class Study2{    private  String no;      // 学号    private  String name; // 姓名}

一、List 转 List

List study1List = Lists.newArrayList();List stringList = study1List.stream().map(Study::getNo ).collect(Collectors.toList());

二、List 转 List 

List study1List = Lists.newArrayList();List studyList = study1List.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet(Comparator.comparing(obj -> obj.getNo()))), ArrayList::new));

三、List 转 List

List study1List = Lists.newArrayList();List study2List = study1List.stream().map(study -> {                Study2 study2= new Study2();                study2.setNo(study.getNo());                study2.setName(study.getName());                return study2;            }).collect(Collectors.toList());

四、List 转 List(普通转换)

// BeanUtils.copyProperties 前提属性,类型必须相同List study1List = Lists.newArrayList();List study2List = Lists.newArrayList();for(Study1 study: study1List ){    Study2 study2 = new Study2();    BeanUtils.copyProperties(study, study2);    study2List.add(study2);}
()

上一篇:轻松搞定,手机下载高德地图的详细指南

下一篇:盗版mc怎么连接服务器?