在两个列表中查找相似但不相等的实体(Java)

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

Finding similar but not equal entities across two lists (Java)

问题

假设我有一个类叫做“Person”,像这样:

public class Person {
   String name;
   int age;
   String address;
   // 获取器和设置器等。
}

我有两个“Person”的列表:一个用于创建,另一个用于删除。

删除列表 =

[
   {
       "name": "David",
       "age": 30,
       "address": "10 Main St."
   },
   {
       "name": "Mary",
       "age": 31,
       "address": "8 Main St."
   },
   {
       "name": "John",
       "age": 40,
       "address": "9 Side St."
   }
]

创建列表 =

[
   {
       "name": "David",
       "age": 30,
       "address": "50 Fleet St."
   },
   {
       "name": "Oliver",
       "age": 31,
       "address": "40 10th St."
   },
   {
       "name": "Jane",
       "age": 40,
       "address": "1 Broadway"
   }
]

我想要获取一对(或一组对),其中左右两边的名称和年龄相同,但地址不同。我的想法是,我有一个要删除的实体列表和一个要创建的实体列表,但是在“David”的情况下,我不想删除该实体并创建一个新实体,因为它有依赖项。我只想更改他的地址。

在“Person”中没有唯一的标识符,因此我不能只通过ID进行比较。

英文:

Let's say I have a class 'Person' like this:

public class Person {
   String name;
   int age;
   String address;
   // Getters and Setters etc.
}

And I have two lists of 'Person': a list to create and a list to delete.

Delete List =

[
   {
       "name: "David"
       "age": 30,
       "address": "10 Main St."
   },
   {
       "name: "Mary"
       "age": 31,
       "address": "8 Main St."
   },
   {
       "name: "John"
       "age": 40,
       "address": "9 Side St."
   }
]

Create List =

[
   {
       "name: "David"
       "age": 30,
       "address": "50 Fleet St."
   },
   {
       "name: "Oliver"
       "age": 31,
       "address": "40 10th St."
   },
   {
       "name: "Jane"
       "age": 40,
       "address": "1 Broadway"
   }
]

I want to get a pair (or list of pairs) where the name and age are the same in the LHS and RHS but the address is different. The idea is that I have a list of entities to delete and list of entities to create, but in the case of David, I don't want to delete the entity and create a new one, because there are dependencies on it. I just want to change his address.

There are no unique identifiers in Person so I cannot just do a comparison by id.

答案1

得分: 1

在现实世界中,您没有足够的信息来判断一条记录是不是不同的人,还是只是一个地址变更。

如果您在一个玩具环境中进行游戏,在这种环境中,您永远不会遇到具有相同姓名和年龄的两个人,那么姓名和年龄就是您的复合主键,您可以基于此编写 hashCode()equals()。在这种情况下,这与 Java 8:使用流检查两个列表中的公共元素 是重复的。

英文:

In the real world, you don't have enough information to decide if a record is a different person or just an address change.

If you are playing in a toy environment where you will never encounter two people with the same name and age, then THAT (name & age) is your composite primary key and you can write a hashCode() and equals() based on that. In which case this is a dup of Java 8: check for common elements in two lists using streams

huangapple
  • 本文由 发表于 2020年9月2日 04:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63695182.html
匿名

发表评论

匿名网友

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

确定