简体   繁体   English

将匹配字段从一个 object 列表复制到 java 中的另一个 object 列表

[英]Copying matching fields from one object list to another object list in java

Hi I have two list of objects.嗨,我有两个对象列表。 List1 and list2列表 1 和列表 2

Employee list from App1来自 App1 的员工列表

list1 
[id=1, name=Thomas, country=null], 
[id=2, name=Smith, country=null], 
[id=3, name=james, country=null], 
[id=4, name=arun, country=null]]

GuestEmployee来宾员工

list2
[id=3, name=null, country=JAPAN, address=abc], 
[id=2, name=null, country=USA, address=def], 
[id=4, name=null, country=ENGLAND, address=hij]]

list1 has name details and list2 has country details. list1 具有名称详细信息,而 list2 具有国家/地区详细信息。 How can I copy country from list2 to list1 for corresponding object with Id as same as list1.如何将 ID 与 list1 相同的相应 object 的国家/地区从 list2 复制到 list1。

In sql its quite easy we can join by ID column and do it.在 sql 中,我们可以很容易地通过 ID 列加入并执行此操作。 But here am hitting seperate API to read list2.但这里我点击单独的 API 来读取 list2。

I can do create map from list2 and have EmpId as key and keep getting object from map and set details to list1.我可以从 list2 创建 map 并将 EmpId 作为键并继续从 map 获取 object 并将详细信息设置为 list1。

Was wondering is there any better approach to do this.想知道有没有更好的方法来做到这一点。

Basically I have to loop two times to get data into list1.基本上我必须循环两次才能将数据放入list1。

Loop1 to create Map and loop2 to copy fileds. Loop1 创建 Map 和 loop2 复制文件。

for(int i =0;i>list2.size();i++){
   list1.get(i).setCountry(list2.get(i).getCountry);
}

Since the size is same, iterate over the second list and set the values of list1由于大小相同,迭代第二个列表并设置 list1 的值

  1. Create class for your entity为您的实体创建 class
  2. Create map HashMap<Integer, YourClass> map;创建 map HashMap<Integer, YourClass> map; for source data list.用于源数据列表。 Use Id field value as a key in map.使用 Id 字段值作为 map 中的键。
  3. Create List<YourClass> list;创建List<YourClass> list; for dest data.对于目标数据。
  4. Iterate your dest list of item and use get method of map to quickly get the country of the Entity with that Id迭代您的 dest 项目列表并使用 map 的get方法快速获取具有该Id的实体的国家/地区
for (YourClass item : list) {
   item.setCountry(map.get(item.getId()));
}

You should iterate both lists and check if the IDs are equal.您应该迭代两个列表并检查 ID 是否相等。

If you want to copy the list2 Country to list1 objects do the following如果要将 list2 Country 复制到 list1 对象,请执行以下操作

for (Employee e1: list1){
 for (Employee e2: list2){
   if (e1.getId()==e2.getId()) { //if its a String do .equals()
      e1.setCountry(e2.getCountry();
   }
 }
}
for(Employee employee : list1){
  for(Employee employee2 : list2) {
    if(employee.getId() == employee2.getId()){
      employee.setCountry(employee2.getCountry());
      break;
    }
  }
}

short: You take every employee from list1, search for an employee with the same id from list2 and set the County if they match.简而言之:您从 list1 中获取每个员工,从 list2 中搜索具有相同 id 的员工,如果匹配,则设置县。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个列表复制到另一个列表 - Copying from one list to another 转换一个可选<list<object> > 到另一个可选<list<object> > 在 Java </list<object></list<object> - Convert one Optional<List<Object>> to another Optional<List<Object>> in Java 从Java中的对象列表中检索字段 - Retrieve fields from object list in Java 在 java 中将备用节点从一个单链表复制到另一个链表 - Copying alternative nodes from one single linked list to another in java 如何通过从另一个对象的列表中累积两个字段来创建对象? - How to create an object by accumulating two fields from a list of another object? 使用Streams将列表对象复制到具有修改内容的另一个列表对象 - copying list object to another list object with modified content using Streams 使用java流从另一个对象列表更新对象列表 - Updating a List of Object from another list of object using java stream Java 8 - 如何在另一个列表中的 object 的列表中设置 object? - Java 8 - How to set an object in a list from an object in another list? 将对象从一个数组列表移动到另一个数组列表 - move an object from one array list to another Java-将一个列表中的对象属性映射到另一个对象列表 - Java - map object properties in one list to another list of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM