合并包含对象的ArraList,无重复,并保留Java中所有相同对象的所有字段值

huangapple go评论81阅读模式
英文:

Merge ArraList of Objects without dublicates and retaining all filed values from all same objects in Java

问题

我有两个对象的ArrayList,如下所示:

ArrayList<Record> dataSetOne;
ArrayList<Record> dataSetTwo;

其中Record对象的结构如下:

public class Record {
    private String id;
    private String name;
    private String homeAddress;
    private String officeAddress;
}

因此,在第一个ArrayList,即dataSetOne,我将获取与家庭地址相关的记录数据。

public class Record {
    id = emp01;
    name = andy;
    homeAddress = mexico;
}

在第二个ArrayList,即dataSetTwo,我将获取与办公地址相关的记录数据。

public class Record {
    id = emp01;
    name = andy;
    officeAddress = california;
}

因此,要求是需要合并这两个ArrayList,根据id作为主键,获取同时包含家庭和办公地址的记录,如下所示:

public class Record {
    id = emp01;
    name = andy;
    homeAddress = mexico;
    officeAddress = california;
}

谢谢

英文:

I Hava two ArrayList of objects like

ArrayList&lt;Record&gt;  dataSetOne;
ArrayList&lt;Record&gt;  dataSetTwo;

Where Records objects looks like

public class Record{
  private String id;
private String name;
private String HomeAdrress;
private String OfficeAdrress;
}

So, In first ArrayList i.e, dataSetOne i will get record data related home adress details.

public class Record{
  id = emp01;
 name = andy;
 HomeAdrress = mexico;
}

In second ArrayList i.e, dataSetTwo i will get record data related office adress details.

public class Record{
   id = emp01;
   name = andy;
   officeAdrress = california;
}

So the requitrement is i need to merge these two arraylist and combine records with id as primary and get both home and office address like below.

public class Record{
  id = emp01;
   name = andy;
  HomeAdrress = mexico;
   officeAdrress = california;
}

Thanks

答案1

得分: 1

import java.util.*;

/* 在这里定义你的 IncompleteRecordErr */
class IncompleteRecordException extends Exception {
    IncompleteRecordException() {    
        super("您的记录不完整。缺少关键数据!");
    }
}

/* */
class Record {
    public int id;
    public String name;
    public String homeAddress;
    public String officeAdrress;
}

public class App {
    public static void main(String[] args) {
        ArrayList<Record> arrList1 = new ArrayList<>();
        ArrayList<Record> arrList2 = new ArrayList<>();
        /* 在上面的列表中填充元素 */
    
        ArrayList<Record> merged = new ArrayList<>();
        for(int i = 0; i < arrList1.size(); i++) { // 假设两个数据列表的长度相同        
            Record r1 = arrList1.get(i);
            Record r2 = arrList2.get(i);
            Record currentRecord = new Record();
            if(r1.id != 0) currentRecord.id = r1.id; // 检查是否为默认值
            else if(r2.id != 0) currentRecord.id = r2.id;
            //else throw new IncompleteRecordException();
    
            if(r1.name != null) currentRecord.name = r1.name;
            else if(r2.name != null) currentRecord.name = r2.name;
            //else throw new IncompleteRecordException();
    
            if(r1.homeAddress != null) currentRecord.homeAddress = r1.homeAddress;
            else if(r2.homeAddress != null) currentRecord.homeAddress = r2.homeAddress;
            //else throw new IncompleteRecordException();
    
            if(r1.officeAdrress != null) currentRecord.officeAdrress = r1.officeAdrress;
            else if(r2.officeAdrress != null) currentRecord.officeAdrress = r2.officeAdrress;
            //else throw new IncompleteRecordException();
    
            merged.add(currentRecord);
        }
    // 现在,"merged" 应该包含合并后的数据
    }
}
英文:
import java.util.*;

/* define your IncompleteRecordErr here */
class IncompleteRecordException extends Exception {
    IncompleteRecordException() {    
        super(&quot;Your record is incomplete. Vital data missing!&quot;);
    }
}

/* */
class Record {
    public int id;
    public String name;
    public String homeAddress;
    public String officeAdrress;
}

public class App {
    public static void main(String[] args) {
        ArrayList&lt;Record&gt; arrList1 = new ArrayList&lt;&gt;();
        ArrayList&lt;Record&gt; arrList2 = new ArrayList&lt;&gt;();
        /* full your arrLists above with elements */
    
        ArrayList&lt;Record&gt; merged = new ArrayList&lt;&gt;();
        for(int i = 0; i &lt; arrList1.size(); i++) { // supposed both data lists have same len        
            Record r1 = arrList1.get(i);
            Record r2 = arrList2.get(i);
            Record currentRecord = new Record();
            if(r1.id != 0) currentRecord.id = r1.id; // check for not default value
            else if(r2.id != 0) currentRecord.id = r2.id;
            //else throw new IncompleteRecordException();
    
            if(r1.name != null) currentRecord.name = r1.name;
            else if(r2.name != null) currentRecord.name = r2.name;
            //else throw new IncompleteRecordException();
    
            if(r1.homeAddress != null) currentRecord.homeAddress = r1.homeAddress;
            else if(r2.homeAddress != null) currentRecord.homeAddress = r2.homeAddress;
            //else throw new IncompleteRecordException();
    
            if(r1.officeAdrress != null) currentRecord.officeAdrress = r1.officeAdrress;
            else if(r2.officeAdrress != null) currentRecord.officeAdrress = r2.officeAdrress;
            //else throw new IncompleteRecordException();
    
            merged.add(currentRecord);
        }
    // now, &quot;merged&quot; should contain your merged data
    }
}

答案2

得分: 0

Record类必须是:

class Record {
    public int id;
    public String name;
    public String homeAddress;
    public String officeAddress;
}

以及主函数:

public static void main(String[] args) {
    ArrayList<Record> merged = new ArrayList<>();
    Record r;
    for (int i = 0; i < dataSetOne.size(); i++) {
        for (int j = 0; j < dataSetTwo.size(); j++) {
            if (dataSetOne.get(i).id == dataSetTwo.get(j).id) {
                r = new Record();
                r.id = dataSetOne.get(i).id;
                r.name = dataSetOne.get(i).name;
                r.homeAddress = dataSetOne.get(i).homeAddress;
                r.officeAddress = dataSetTwo.get(j).officeAddress;
                merged.add(r);
            }
        }
    }
}
英文:

Record class must be :

class Record{
public int id;
public String name;
public String homeAddress;
public String officeAdrress;
}

and main function :

public static void main(String[] args) {
ArrayList&lt;Record&gt; merged = new ArrayList&lt;&gt;();
Record r;
for(int i = 0; i &lt; dataSetOne.size(); i++) {
for(int j = 0; j &lt; dataSetTwo.size(); j++){
if(dataSetOne.get(i).id == dataSetTwo.get(j).id){
r = new Record();
r.id = dataSetOne.get(i).id;
r.name=dataSetOne.get(i).name;
r.homeAddress = dataSetOne.get(i).homeAddress;
r.officeAdrress = dataSetTwo.get(j).officeAdrress;
merged.add(r);
}
}
}
}

huangapple
  • 本文由 发表于 2020年5月5日 17:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61609568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定