In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to find the difference between the two lists in Java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Overview
Finding differences between collections of objects of the same data type is a common programming task. For example, suppose we have a list of students who apply for the exam and another list of students who have passed the exam. The difference between the two lists will tell us the students who failed the exam.
In Java, there is no explicit method in List API to find the difference between two lists, although there are some helper methods that are very close.
two。 Test Settin
First define two lists, which we will use to test the example:
Public class FindDifferencesBetweenListsUnitTest {private static final List listOne = Arrays.asList ("Jack", "Tom", "Sam", "John", "James", "Jack"); private static final List listTwo = Arrays.asList ("Jack", "Daniel", "Sam", "Alan", "James", "George");}
3. Use Java List API
We can create a copy of the list and then use List's method removeAll () to delete all the same elements as another:
List differences = new ArrayList (listOne); differences.removeAll (listTwo); assertEquals (2, differences.size ()); assertThat (differences) .containsExactly ("Tom", "John")
Let's turn this upside down and find out the difference from another angle:
List differences = new ArrayList (listTwo); differences.removeAll (listOne); assertEquals (3, differences.size ()); assertThat (differences) .containsExactly ("Daniel", "Alan", "George")
We should also note that if we want to find the common element between the two lists, List also has a retainal method.
4. Use Streams API
Java Stream API can be used to perform sequential operations on the data in the collection, including filtering differences between lists:
List differences = listOne.stream () .filter (element->! listTwo.contains (element)) .filter (Collectors.toList ()); assertEquals (2, differences.size ()); assertThat (differences) .containsExactly ("Tom", "John")
As in the first example, we can switch the order of the list to find different elements from the second list:
List differences = listTwo.stream () .filter (element->! listOne.contains (element)) .filter (Collectors.toList ()); assertEquals (3, differences.size ()); assertThat (differences) .containsExactly ("Daniel", "Alan", "George")
Note that List.contains () can be a costly operation for a larger list.
5. Use third-party libraries
5.1. Use Google Guava
Guava contains the Sets.difference method, but to use it, we need to first convert the list to a collection:
List differences = new ArrayList (Sets.difference (Sets.newHashSet (listOne), Sets.newHashSet (listTwo)); assertEquals (2, differences.size ()); assertThat (differences). Containers ExactlyInAnyOrder ("Tom", "John")
Note that converting a list to a collection has the effect of deduplication and reordering.
5.2. Use Apache Commons Collections
The CollectionUtils in Apache Commons Collections contains the removeAll method.
This method is similar to List.removeAll (), but also creates a new collection for the result:
List differences = new ArrayList ((CollectionUtils.removeAll (listOne, listTwo)); assertEquals (2, differences.size ()); assertThat (differences) .containsExactly ("Tom", "John")
6. Deal with duplicate values
Now let's look at the difference when two lists contain duplicate values.
To do this, we need to remove duplicate elements from the first list exactly to the number of times they are contained in the second list
In our example, the "Jack" value appears twice in the first list and only once in the second list:
List differences = new ArrayList (listOne); listTwo.forEach (differences::remove); assertThat (differences) .containsExactly ("Tom", "John", "Jack")
We can also use the subtract method in Apache Commons Collections to implement:
List differences = new ArrayList (CollectionUtils.subtract (listOne, listTwo)); assertEquals (3, differences.size ()); assertThat (differences) .containsExactly ("Tom", "John", "Jack"); and "how to find the difference between two lists in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.