Finding the different elements between two ArrayLists in Java

后端 未结 6 1265
滥情空心
滥情空心 2021-02-06 05:43

How can I know the different element between 2 array list in java? I need the exact element not a Boolean value which can be retrieved using removeAll().

6条回答
  •  北海茫月
    2021-02-06 06:18

    It depends on what do you want to check.

    1. If you want to get all the unique elements for both lists (i.e. sum of all elements that are unique for the first list and all elements that unique for the second list) also known as symmetric difference you can use as mentioned above disjunction method from Apache Commons Collections 4.0:

      CollectionUtils.disjunction(a, b);
      
    2. If you want to get all unique elements only from one list (i.e. elements that exist only in one list, but don't exist in the other) also known as relative complement you can subtract from this list the other one using subtract method from Apache Commons Collections 4.0:

      CollectionUtils.subtract(a, b); //gives all unique elements of a that don't exist in b
      

提交回复
热议问题