current position:Home>The removeall() and addall() methods in the list set are the same as the list What is the difference between the difference set and the sum set of two sets?

The removeall() and addall() methods in the list set are the same as the list What is the difference between the difference set and the sum set of two sets?

2022-02-03 01:25:08 CSDN Q & A

###### Happened to use it

public class Test {
public static void main(String[] args) {
List list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
List list2=new ArrayList();
list2.add(2);
list2.add(4);
list2.add(6);

List<Integer> list1Cha = list1.stream().filter(item -> list2.stream().noneMatch(item2 -> Objects.equals(item, item2))).collect(Collectors.toList());List<Integer> list2Cha = list2.stream().filter(item -> list1.stream().noneMatch(item2 -> Objects.equals(item, item2))).collect(Collectors.toList());    System.out.println(list1Cha);    System.out.println(list2Cha);    List<Integer> heList = list1.stream().filter(item -> list2.stream().anyMatch(item2 -> Objects.equals(item, item2))).collect(Collectors.toList());    System.out.println(heList);   list1.addAll(list2);   System.out.println(list1);    list1.removeAll(list2);    System.out.println(list1);}  

}
Do not paste screenshots

Operation results and error reports

img


Content

List list3=new ArrayList();
    list3.addAll(list1);    list1.removeAll(list2);    System.out.println(list1);    list2.removeAll(list3);    System.out.println(list2);    boolean b = list1.equals(list1Cha);    System.out.println(b);
What I want to achieve



Refer to the answer 1:

addall No matter what you repeat , Just put them together , Finding a collection will automatically remove duplicates
removeall If it doesn't have , I don't know if there will be an error , But when finding the difference set, even if there is no error, it will not report an error




Refer to the answer 2:



Refer to the answer 3:

One 、 I have some questions :
1. What are the criteria for determining that the elements in the set are the same ? The values are equal ? Is the same object ?
2. Whether the difference between set and list is clear ? aggregate : Element does not have the same element ; list : The same elements are allowed .
Two 、 explain list Of removeAll() And addAll() And list.stream() Find the difference between the difference set and the sum set of two sets
1.list Of list1.removeAll(list2), from list1 Delete all in list2 The elements that exist in
2.list Of list2.addAll(list2), since list1 Tail add list2 All elements in , Does not verify the existence of the same element
3. The code implementation is in line with your understanding of the calculation , If the logic is correct , In line with your understanding of Set Computing
4. If you want to use set calculation , If you don't want to write your own code, you can consider Set Implementation class of interface




Refer to the answer 4:



Refer to the answer 5:

Just look at the source code




Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030125063772.html

Random recommended