I have the following two lists which contain objects with the following properties. Data has been included in examples.
List 1 - ShopOwnerMapList
+------------+-------------+
| Owner | ShopId |
+------------+-------------+
| Jack | 67 |
| Sarah | 69 |
| Sarah | B7 |
| Tom | 83 |
| Harry | 20 |
+------------+-------------+
ShopOwner to ShopIds is a "one to many" relationship. One ShopOwner can have many ids.
List 2 - ShopOwnerList
+------------+------+--------+
| Owner | Age | ShopId |
+------------+------+--------+
| Jack | 32 | NULL |
| Sarah | 30 | NULL |
| Tom | 45 | NULL |
| Harry | 55 | NULL |
+------------+------+--------+
ShopOwnerList has a property called ShopId which is NULL by default.
How do I now map ShopOwnerMapList to ShopOwnerList using Owner as "key" of sorts? If two instances of Owner exist in ShopOwnerMapList (as with Sarah), then I want the corresponding item in ShopOwnerList to be duplicated. So in effect, after the merge I would end up with:
+------------+------+--------+
| Owner | Age | ShopId |
+------------+------+--------+
| Jack | 32 | 67 |
| Sarah | 30 | 69 |
| Sarah | 30 | B7 |
| Tom | 45 | 83 |
| Harry | 55 | 20 |
+------------+------+--------+
How can I accomplish this in Java?
I've tried the following but it doesn't extend ShopOwnerList in a case where two instances of Owner exist:
for (int i = 0; i < shopOwnerMapList.size(); i++) {
for (int j = 0; j < shopOwnerList.size(); j++) {
ShopOwnerMap shopOwnerMap = shopOwnerMapList.get(i);
ShopOwner shopOwner = shopOwnerList.get(j);
if(shopOwnerMap.getOwner().equals(shopOwner.getOwner()) {
if(shopOwner.getShopId() == null) {
shopOwner.setShopId(shopOwnerMap.getShopId());
}
}
}
}
Classes
public class ShopOwnerMap {
String Owner;
String ShopId;
public MyClass() {
}
public String getOwner() {
return this.Owner;
}
public void setOwner(String value) {
this.Owner = value;
}
public String getShopId() {
return this.ShopId;
}
public void setShopId(String value) {
this.ShopId = value;
}
}
public class ShopOwner {
String Owner;
String ShopId;
Integer Age;
String DateOfBirth;
public ShopOwner() {
}
public String getOwner() {
return this.Owner;
}
public void setOwner(String value) {
this.Owner = value;
}
public String getShopId() {
return this.ShopId;
}
public void setShopId(String value) {
this.ShopId = value;
}
public Integer getAge() {
return this.Age;
}
public void setAge(Integer value) {
this.Age = value;
}
public String getDateOfBirth() {
return this.DateOfBirth;
}
public void setDateOfBirth(String value) {
this.DateOfBirth = value;
}
}
ShopOwnerListtoShopOwnerMapList?shopOwnerMap.getOwner() == shopOwner.getOwner()as you're checking reference equality, not object equality. Try changing that toshopOwnerMap.getOwner().equals(shopOwner.getOwner())