Java 一个数组集合List<People> 赋值给另一个数组集合List<NewPeople> ,两个数组集合属性部分一致。

Java 一个数组集合List 赋值给另一个数组集合List ,两个数组集合属性部分一致。

下面是一个Demo, 具体要根据自己的业务调整。

java 复制代码
import java.util.ArrayList;
import java.util.List;

class People {
    private String name;
    private int age;
    private String address;

    public People(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    // 省略getter和setter方法
}

class NewPeople {
    private String name;
    private int age;

    public NewPeople(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 省略getter和setter方法
}

public class Main {
    public static void main(String[] args) {
        List<People> peopleList = new ArrayList<>();
        peopleList.add(new People("张三", 30, "北京"));
        peopleList.add(new People("李四", 20, "上海"));
        peopleList.add(new People("王五", 50, "广州"));

        List<NewPeople> newPeopleList = new ArrayList<>();
        for (People people : peopleList) {
            NewPeople newPeople = new NewPeople(people.getName(), people.getAge());
            newPeopleList.add(newPeople);
        }

        // 输出newPeopleList
        for (NewPeople newPeople : newPeopleList) {
            System.out.println("姓名:" + newPeople.getName() + ",年龄:" + newPeople.getAge());
        }
    }
}
相关推荐
Once_day4 分钟前
C++之《程序员自我修养》读书总结(1)
c语言·开发语言·c++·程序员自我修养
若鱼19196 分钟前
SpringBoot4.0新特性-Observability让生产环境更易于观测
java·spring
倒流时光三十年9 分钟前
SpringBoot 数据库同步 Elasticsearch 性能优化
数据库·spring boot·elasticsearch
觉醒大王15 分钟前
强女思维:着急,是贪欲外显的相。
java·论文阅读·笔记·深度学习·学习·自然语言处理·学习方法
喜欢喝果茶.22 分钟前
QOverload<参数列表>::of(&函数名)信号槽
开发语言·qt
亓才孓23 分钟前
[Class类的应用]反射的理解
开发语言·python
努力学编程呀(๑•ี_เ•ี๑)23 分钟前
【在 IntelliJ IDEA 中切换项目 JDK 版本】
java·开发语言·intellij-idea
码农小卡拉32 分钟前
深入解析Spring Boot文件加载顺序与加载方式
java·数据库·spring boot
向上的车轮40 分钟前
为什么.NET(C#)转 Java 开发时常常在“吐槽”Java:checked exception
java·c#·.net
Dragon Wu41 分钟前
Spring Security Oauth2.1 授权码模式实现前后端分离的方案
java·spring boot·后端·spring cloud·springboot·springcloud