The difference between removeAll() and clear() in ArrayList
boolean removeAll(Collection<?> c)
Remove all elements of a given collection from the list (optional action).
removeAll(Collection< ?> C)
can pass a Collection type parameter, which is intended to clear the current list with all the elements contained in the passed Collection. That is to say, removeAll is actually an operation that first intersects with c and then fills.
And clear() clears all the elements in the current list. Although the names are somewhat similar, they are actually completely different operations.
Note:
-
- removeAll is similar to retainAll by taking the intersection of the current list and the incoming Collection. That is to say, the current list does not necessarily contain all the elements of the incoming Collection. The incoming Collection may contain some elements that are not in the current list, but it does not affect the work of removeAll and retainAll. Most collections can use these two methods and pass the Collection as a parameter, not just to List or ArrayList, but to be aware of element type compatibility. RemoveAll removes all elements from the current list that intersect with the passed Collection, rather than just removing the first element from the list, as removeAll does. So, when you want to removeAll the element a from the list, you can use removeAll:
arrayList.removeAll(new ArrayList<Integer>(Arrays.asList(a)));
RetainAll is similar to removeAll in that when an element a is included in the passed Collection, multiple a’s in the list retain both removeAll and retainAll’s behavior depending on the equals() method. You must be aware of the changes
‘s behavior when using
The attached:
boolean retainAll(Collection<?> c)
Keep only those elements of this collection that are also included in the specified collection (optional action). In other words, remove all elements from this collection that are not included in the specified collection.
Read More:
- OSS Error: The difference between the request time and the current time is too large
- [Solved] minio Failed to Upload File Error: The difference between the request time and the server‘s time is too large.
- Ubuntu20.04 install the ROS noetic version in catkin_Problems in make compilation
- The upgrade of Ubuntu results in an error in the compilation of Android Jack [Two Method to Solve]
- Solutions to errors in installing xunsearch engine in centos8.0
- Vector series in actual C + +_ To_ fit()
- [Solved] UnicodeEncodeError: ‘ascii‘ codec can‘t encode characters in position 3-9: ordinal not in range(128)
- When installing software in Ubuntu, it prompts: E: You don’t have enough free space in /var/cache/apt/archives/.
- How to Create Threads in Linux
- Linux apt-get update Error: Error in `appstreamcli‘: double free or corruption … Aborted (core dumped)
- [Solved] Centos7 Error: gcc: configure: error: in build‘: configure: error: no acceptable C compiler found in $PATH
- [Solved] Error :: You must put some ‘source‘ URIs in your sources.list
- Installation and configuration of redis in Linux
- How to Delete New Memory in Vector
- Detailed explanation of basic lstmcell in tensorflow learning
- How to Solve Xmind Install Error in Ubuntu System
- CentOS 8: No URLs in mirrorlist error [How to Solve]
- Grep: How to Find All the Files Containing a String in Linux
- How to Use Annotations in Flutter & Dart
- Restart and stop of nginx in Linux service