[JAVA] Find the difference between List types

I wish I could create something like the SQL minus operator (← transmitted) between List types, and when I looked it up, Apache Commons was useful.

Sample.java


List<String> list1 = Arrays.asList("a", "b", "e", "f");
List<String> list2 = Arrays.asList("a", "b", "c", "d", "e");

//Extract what is in list1 but not in list2
@SuppressWarnings("unchecked")
Collection<String> result = CollectionUtils.subtract(list1, list2);

System.out.println(result.toString()); // => [f]

As for the contents of CollectionUtils # subtract (), it seems that ArrayList is created based on the first argument (list1), and the second argument (list2) is rotated around with an iterator to remove one by one. It seems that the order of one argument (list1) is guaranteed. However, I feel that it is correct to pack it in the appropriate data type.

I was about to mass-produce a simple and troublesome code like ↓.

python


List<String> result = new ArrayList<String>(list1);
result.removeAll(list2);

Recommended Posts

Find the difference between List types
[Java] Understand the difference between List and Set
Difference between List and ArrayList
Difference between Java and JavaScript (how to find the average)
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Find the angle between two vectors
Find the average age from List <Person>.
About the difference between irb and pry
Find the difference from a multiple of 10
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
[Android] Easily calculate the difference between two dates
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
Regarding the difference between the three Timeouts in Java's HttpClient
About the difference between classes and instances in Ruby
Calculate the difference between numbers in a Ruby array
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
Difference between vh and%
[Rails] I investigated the difference between redirect_to and render.
Difference between i ++ and ++ i
What is the difference between Java EE and Jakarta EE?
[Swift] UITextField taught me the difference between nil and ""
[Rails] What is the difference between bundle install and bundle update?
About the difference between "(double quotation)" and "single quotation" in Ruby
Difference between element 0, null and empty string (check in list)
[Ruby] About the difference between 2 dots and 3 dots of range object.
What is the difference between an action and an instance method?
[Java] Check the difference between orElse and orElseGet with IntStream
Let's override the difference between == (identity) and equals method (equivalence)
The difference between programming with Ruby classes and programming without it
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
[Ruby] Difference between match / scan
Android Spot the Difference Game
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
Consider implementing a method that returns the difference between two Lists
About the difference between gets and gets.chomp (other than line breaks)
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].