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());
        }
    }
}
相关推荐
北极无雪20 分钟前
Spring源码学习(拓展篇):SpringMVC中的异常处理
java·开发语言·数据库·学习·spring·servlet
VXbishe27 分钟前
(附源码)基于springboot的“我来找房”微信小程序的设计与实现-计算机毕设 23157
java·python·微信小程序·node.js·c#·php·课程设计
猿小猴子31 分钟前
Python3 爬虫 中间人爬虫
开发语言·爬虫·python
lly20240639 分钟前
Swift 方法
开发语言
YONG823_API1 小时前
电商平台数据批量获取自动抓取的实现方法分享(API)
java·大数据·开发语言·数据库·爬虫·网络爬虫
寂柒1 小时前
C++——模拟实现stack和queue
开发语言·c++·算法·list
扬子鳄0081 小时前
java注解的处理器
java
Amagi.1 小时前
Spring中Bean的作用域
java·后端·spring
2402_857589361 小时前
Spring Boot新闻推荐系统设计与实现
java·spring boot·后端
繁依Fanyi1 小时前
旅游心动盲盒:开启个性化旅行新体验
java·服务器·python·算法·eclipse·tomcat·旅游