Java:List转换List

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

@Data
public class Study1{
    private  String no;      // 学号
    private  String name;	 // 姓名
}

@Data
public 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);
}

你可能感兴趣的:(Java,java,list,开发语言)