The source code for clear()
:
public void clear() { modCount++; // Let gc do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; }
The source code for removeAll()
(As defined in AbstractCollection
):
public boolean removeAll(Collection<?> c) { boolean modified = false; Iterator<?> e = iterator(); while (e.hasNext()) { if (c.contains(e.next())) { e.remove(); modified = true; } } return modified; }
clear()
is much faster since it doesn’t have to deal with all those extra method calls.
And as Atrey points out, c.contains(..)
increases the time complexity of removeAll
to O(n2) as opposed to clear
‘s O(n).
Related Posts:
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- When to use LinkedList over ArrayList in Java?
- Convert list to array in Java [duplicate]
- Initialization of an ArrayList in one line
- How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList
- How to sort an ArrayList?
- What’s the C++ version of Java’s ArrayList
- Create ArrayList from array
- Best way to convert an ArrayList to a string
- “Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0″ with ArrayList?
- Best way to convert an ArrayList to a string
- Print ArrayList
- Print ArrayList
- How to make a deep copy of Java ArrayList
- Array ArrayList python equivalent
- Java ArrayList copy
- how to iterate in List
- > in java and set their values as we do in a normal int a[i][j] matrix type [duplicate]
- How to convert an ArrayList containing Integers to primitive int array?
- Print ArrayList
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- HashSet vs. ArrayList
- Java ArrayList replace at specific index
- How can I create an Array of ArrayLists?
- How to create an 2D ArrayList in java?
- Error: cannot find symbol ArrayList
- java howto ArrayList push, pop, shift, and unshift
- How to get the last value of an ArrayList
- How to add an object to an ArrayList in Java
- Add String Array to ArrayList
- Incompatible types List of List and ArrayList of ArrayList
- Size has private access in ArrayList
- How to write a test class to test my code?
- How to convert a String into an ArrayList?
- Java ArrayList of Doubles
- How to create a Multidimensional ArrayList in Java?
- convert string to arraylist
in java - ShoppingCart.Java Program Assignment
- How Do I Implement an Insertion Sort Method for A Generic ArrayList?
- toring and Retrieving ArrayList values from hashmap
- return an ArrayList method
- How do I resolve ClassNotFoundException?
- Editor does not contain a main type in Eclipse [duplicate]
- How to replace � in a string
- How to decrypt a SHA-256 encrypted string?
- How to write a Unit Test?
- Iterate through a HashMap [duplicate]
- Java optional parameters
- Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?
- ‘mvn’ is not recognized as an internal or external command, operable program or batch file
- Convert an integer to an array of characters : java
- How to compare dates in Java?
- Java integer-double division confusion
- Example of O(n!)?
- A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found
- How to efficiently remove duplicates from an array without using Set
- How to master Java?
- Understanding the difference between null and ‘\u000’ in Java
- How do you make a deep copy of an object?
- what does Dead Code mean under Eclipse IDE Problems Section
- java, get set methods
- How to solve DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502
- Javadoc @see or {@link}?
- Remote debugging a Java application
- Range of valid character for a base 64 encoding
- How to parse JSON boolean value?
- Insert Dimensions to complete Expression/ReferenceType
- Unable to locate tools.jar
- Possible lossy conversion from double to float, given float values?
- how to fix EXE4J_JAVA_HOME, No JVM could be found on your system error?
- Error: ‘else’ without ‘if’
- How to create a custom exception type in Java?
- Error: JavaFX runtime components are missing, and are required to run this application with JDK 11
- All inclusive Charset to avoid “java.nio.charset.MalformedInputException: Input length = 1”?
- javac error: Class names are only accepted if annotation processing is explicitly requested
- JOptionPane YES NO OPTION
- How to check if a String is numeric in Java
- How to get today’s Date?
- Creating a random string with A-Z and 0-9 in Java
- java.lang.ArithmeticException: / by zero
- How can I convert a .jar to an .exe?
- Why doesn’t RecyclerView have onItemClickListener()?
- Inverse Logistic Function / Reverse Sigmoid Function
- How can I check if a single character appears in a string?
- Java Error – Illegal Modifier for Parameter – Only final Permitted
- Fatal error compiling: tools.jar not found:
- Invalid initial heap size. Could not create the Java virtual machine
- implicit super constructor object() is undefined. must explicitly invoke another constructor
- Making a deep copy of a LinkedList in java
- How do you set one array’s values to another array’s values in Java?
- ‘No JUnit tests found’ in Eclipse
- Popup Message boxes
- What is the purpose of the default keyword in Java?
- What exactly is Ajax request? Is it different from Servlet Request?
- java ‘jar’ is not recognized as an internal or external command
- A JRE or JDK must be available in order to run Eclipse. No JVM was found after searching the following locations
- Why cannot cast Integer to String in java?
- java.util.ConcurrentModificationException with iterator
- How to read file from relative path in Java project? java.io.File cannot find the path specified
- How can I view and edit specific aspects of playerdata from my Minecraft server?
- String index out of bounds exception java