如何比较两个不同大小的 Java 列表对象

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

How to compare two different sizes List Objects Java

问题

  1. 我有两个列表列表A和列表B它们的大小不同列表A从文件中解析而列表B从数据库中获取数据
  2. class A{
  3. private String id;
  4. private String mobile;
  5. }
  6. class B{
  7. private String id;
  8. private String name;
  9. private String address;
  10. private String mobile;
  11. private String pincode;
  12. }
  13. 现在我想要**比较**这两个列表并且想要从**列表A**中删除与**列表B**中具有相同手机号的相应id
  14. **尝试了以下代码**
  15. ```java
  16. private List<A> compareList(List<A> listA, List<B> listB){
  17. List<A> temp = new ArrayList<>();
  18. for(A a : listA){
  19. for(B b : listB){
  20. if(a.getId().equals(b.getId()) && !a.getMobile().equals(b.getMobile())){
  21. temp.add(a);
  22. }
  23. }
  24. }
  25. return temp;
  26. }

有人可以指导我吗?

  1. <details>
  2. <summary>英文:</summary>
  3. I have two Lists, List A and List B having different sizes. List A is getting parsed from file and List B is fetching data from the database.

class A{
private String id;
private String mobile;
}

class B{
private String id;
private String name;
private String address;
private String mobile;
private String pincode;
}

  1. Now I want to **Compare** both the list and want to remove the respective ids from **List A** which are having the same mobile number as **ListB**.
  2. **Tried below code**

private List<A> compareList(List<A> listA, List<B> listB){
List<A> temp = new ArrayList<>();
for(A a : listA){
for(B b : listB){
if(a.getId().equals(b.getId()) && !a.getMobile().equals(b.getMobile())){
temp.add(a);
}
}
}
return temp;
}

  1. Can someone please guide me?
  2. </details>
  3. # 答案1
  4. **得分**: 3
  5. 你的方法会创建一个新列表,而不是从现有列表中删除项目。假设你真的想要删除项目,这是你可以使用Java 8 streams API执行的一种方式:如果listB中的某个项目具有与listA中的某个项目相同的“mobile”,则从listA中删除一个项目:
  6. ```java
  7. listA.removeIf(a -> listB.stream()
  8. .anyMatch(b -> Objects.equals(a.getMobile(), b.getMobile())));

在这种情况下,使用流的API可能有些难以阅读。这里是同样的操作,但不使用流:

  1. for (B b : listB) {
  2. listA.removeIf(a -> Objects.equals(a.mobile, b.mobile));
  3. }
英文:

Your method creates a new list, instead of removing items from the existing list. Assuming you actually want to remove items, this is one way you can do it with the Java 8 streams API: remove an item from listA if it has the same mobile as an item in listB:

  1. listA.removeIf(a -&gt; listB.stream()
  2. .anyMatch(b -&gt; Objects.equals(a.getMobile(), b.getMobile())));

The streams API in this case is a little difficult to read. Here's the same without using a stream:

  1. for (B b : listB) {
  2. listA.removeIf(a -&gt; Objects.equals(a.mobile, b.mobile));
  3. }

答案2

得分: 1

你可以使用一个标志来表示存在性,并且如果不存在,将元素添加到临时列表中,那么临时列表只包含那些在 A 中存在但在 B 中不存在的元素。

  1. List<A> temp = new ArrayList<>();
  2. for (A a : listA) {
  3. boolean isExist = false;
  4. for (B b : listB) {
  5. if (a.getMobile().equals(b.getMobile())) {
  6. isExist = true; // 如果存在于 B 列表中
  7. break;
  8. }
  9. }
  10. if (!isExist) { // 如果在 B 中不存在,则添加到列表中
  11. temp.add(a);
  12. }
  13. }

注意: 在问题中,您只说要比较手机号码,但在代码中您还在比较ID,如果您不想要ID相等的检查,请将ID相等的部分删除。

英文:

You can use a flag for existence and add in temp if not exists then temp contains only those elements of A which do not exist in B

  1. List&lt;A&gt; temp = new ArrayList&lt;&gt;();
  2. for(A a : listA){
  3. boolean isExist = false;
  4. for(B b : listB){
  5. if(a.getId().equals(b.getId()) &amp;&amp; a.getMobile().equals(b.getMobile())){
  6. isExist = true; // if exist in List of B
  7. break;
  8. }
  9. }
  10. if(!isExist){ // if not exist in B then add in list
  11. temp.add(a);
  12. }
  13. }

Note: In question you say to compare only mobile number but in code you are comparing id also, remove id equal check if you don't want it

答案3

得分: 0

我认为你应该将手机分开存放,因为它们很可能会重复。之后,将主要集合与这个手机列表进行比较。

  1. private static List<A> compareList(List<A> listA, List<B> listB) {
  2. List<String> mobiles = listB.stream()
  3. .map(B::getMobile)
  4. .distinct()
  5. .collect(Collectors.toList());
  6. return listA.stream()
  7. .filter(entity -> !mobiles.contains(entity.getMobile()))
  8. .collect(Collectors.toList());
  9. }
英文:

I think you should keep the mobiles separately, as they can probably be repeated. And after that compare the main collection with this list of mobiles.

  1. private static List&lt;A&gt; compareList(List&lt;A&gt; listA, List&lt;B&gt; listB) {
  2. List&lt;String&gt; mobiles = listB.stream()
  3. .map(B::getMobile)
  4. .distinct()
  5. .collect(Collectors.toList());
  6. return listA.stream()
  7. .filter(entity -&gt; !mobiles.contains(entity.getMobile()))
  8. .collect(Collectors.toList());
  9. }

答案4

得分: 0

  1. **创建新的筛选列表**
  2. 如果条件不匹配则将所有元素收集到新列表中
  3. List<A> filteredList =
  4. aList.stream()
  5. .filter(Predicate.not(a -> bList.stream().anyMatch(b -> a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile()))))
  6. .collect(Collectors.toList());
  7. **用于原地替换**
  8. aList.removeIf(a -> bList.stream().anyMatch(b -> a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile())));
英文:

To create new filtered List

Collect all element in new list if condition does not match

  1. List&lt;A&gt; filteredList =
  2. aList.stream()
  3. .filter(Predicate.not(a -&gt; bList.stream().anyMatch(b -&gt; a.getId().equals(b.getId()) &amp;&amp; a.getMobile().equals(b.getMobile()))))
  4. .collect(Collectors.toList());

For in place replacement

  1. aList.removeIf(a -&gt; bList.stream().anyMatch(b -&gt; a.getId().equals(b.getId()) &amp;&amp; a.getMobile().equals(b.getMobile())));

huangapple
  • 本文由 发表于 2020年8月15日 18:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63425167.html
匿名

发表评论

匿名网友

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

确定