Java - 将一个列表中的对象属性映射到另一个对象列表

时间:2018-05-16 21:28:01

标签: java

我有以下两个列表,其中包含具有以下属性的对象。数据已包含在示例中。

列表1 - ShopOwnerMapList

+------------+-------------+
| Owner      | ShopId      |
+------------+-------------+
| Jack       | 67          |
| Sarah      | 69          |
| Sarah      | B7          |
| Tom        | 83          |
| Harry      | 20          |
+------------+-------------+

ShopOwner到ShopIds是一对多的"关系。一个ShopOwner可以拥有许多ID。

列表2 - ShopOwnerList

+------------+------+--------+
| Owner      | Age  | ShopId |
+------------+------+--------+ 
| Jack       | 32   | NULL   | 
| Sarah      | 30   | NULL   |
| Tom        | 45   | NULL   |
| Harry      | 55   | NULL   |
+------------+------+--------+

ShopOwnerList有一个名为ShopId的属性,默认为NULL

我现在如何使用Owner as" key"将ShopOwnerMapList映射到ShopOwnerList。各种各样的?如果ShopOwnerMapList中存在两个Owner实例(与Sarah一样),那么我希望ShopOwnerList中的相应项被复制。所以实际上,在合并之后我最终会得到:

+------------+------+--------+
| Owner      | Age  | ShopId |
+------------+------+--------+ 
| Jack       | 32   | 67     | 
| Sarah      | 30   | 69     |
| Sarah      | 30   | B7     |
| Tom        | 45   | 83     |
| Harry      | 55   | 20     |
+------------+------+--------+

如何在Java中实现这一目标?

我已尝试过以下操作,但在存在两个所有者实例的情况下,它不会扩展ShopOwnerList:

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());            
            }
        }
    }
}

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;
   }
}

2 个答案:

答案 0 :(得分:1)

正如我在评论中所说,我建议你重新考虑你的模型。 首先介绍一个Shop类:

public class Shop {
    private final String id; // Id of this shop.
    private final ShopOwner owner; // Owner of this shop.
    public Shop(String id, ShopOwner owner) {
        this.id = id;
        this.owner = owner;
    }
    public String getId() { return id; }

    // Overwrite equals and hashcode to allow using Shop as a key in a HashMap (only necessary if you want to enforce uniqueness of a ShopOwner's shops by maintaining a ShopOwner's owned shops using a Map).
    // We simply define equality based on id equality and rely on String.hashCode() (note that this will have to be changed if you want to add additional properties and define equality based on those):
    @Override
    public boolean equals(Object o) {
        return o instanceof Shop && o.toString().equals(this.toString());
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }
    @Override
    public String toString() {
        return id;
    }
    // Other properties etc.
}

更改ShopOwner以保留此List拥有的ShopOwner个商店:

public class ShopOwner {
   String owner;
   List<Shop> shops = new ArrayList<>(); // Shops owned by this ShopOwner.
   int age;     
   String dateOfBirth;
   public void addShop(Shop s) {
       // Note that this does NOT guarantee uniqueness of shops if your DB query returns multiple instances of the same shop.
       // If you want this, you could use a Map instead.
       this.shops.add(s);
   }
   // Your other getters etc.
}

根据您的意见,由于您与数据库的交互方式,您希望保留ShopOwnerMap(我个人会摆脱这种情况,只是实例化虚拟(临时; owner字段设置为nullShop及其各自的虚拟(读取:临时 - 稍后将被匹配代码中找到的“真实”(已存在)ShopOwner实例替换){{1}立即,但这需要更多的知识,如何导致这个问题的代码看起来像。您现在要做的是将ShopOwnerList<ShopOwnerMap>匹配,并将List<ShopOwner>个实例添加到Shop中的每个ShopOwner,如下所示:

List<ShopOwner>

免责声明:我很累,有点匆忙,所以它可能不是100%正确,但它应该为您提供一般的想法。此外 - 正如上面括号中的评论所示 - 这个提议的设计并不完美。这需要更深入地了解您的数据模型等。

答案 1 :(得分:0)

我建议使用番石榴的Multimap。您可以使用键将多个值插入多图中的单个位置。要实现您的解决方案,您基本上需要两个地图。一个名为ShopOwnerMultimap的Multimap,它包含ShopOwnerMapList中的数据以及一个名为ShopOwnerMap的常规Map,其中包含ShopOwnerList中的数据。两个地图都将使用所有者名称作为密钥。执行映射时,使用ShopOwnerMap中的键集从ShopOwnerMultimap中获取值。

相关问题