When its comes to the behavior of ArrayList
and HashSet
they are completely different classes.
ArrayList
ArrayList
Does not validate duplicates.get()
isO(1)
contains()
isO(n)
but you have fully control over the order of the entries.get add contains next remove(0) iterator.remove ArrayList O(1) O(1) O(n) O(1) O(1) O(1)
- Not thread safe and to make it thread safe you have to use
Collections.synchronizedList(...)
HashSet
HashSet
ensures there are no duplicates.- Gives you an
O(1)
contains()
method but doesn’t preserve order.add contains next notes HashSet O(1) O(1) O(h/n) h is the table
- Not thread safe and to make it thread safe you have to use
Collections.synchronizedSet(...)
Related Posts:
- How to sort a HashSet?
- When to use LinkedList over ArrayList in Java?
- Initialization of an ArrayList in one line
- How to sort an ArrayList?
- Difference between HashSet and HashMap?
- How to calculate the intersection of two sets?
- Union or intersection of Java Sets
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- Difference between Arrays.asList(array) and new ArrayList
(Arrays.asList(array)) - Convert list to array in Java [duplicate]
- How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from 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?
- Convert Set to List without creating new List
- 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
- Why is there no SortedList in Java?
- 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?
- Printing HashMap In Java
- java, get set methods
- Print ArrayList
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- FIFO based Queue implementations?
- Java ArrayList replace at specific index
- How can I create an Array of ArrayLists?
- Create a List of primitive int?
- How to create an 2D ArrayList in java?
- Error: cannot find symbol ArrayList
- How to convert int[] to Integer[] in Java?
- Change priorityQueue to max priorityqueue
- Collections.emptyMap() vs new HashMap()
- java howto ArrayList push, pop, shift, and unshift
- Sort a Map
by values - Any implementation of Ordered Set in Java?
- Any implementation of Ordered Set in Java?
- 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
- Why is a ConcurrentModificationException thrown and how to debug it
- 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?
- What’s the difference between HashSet and Set?
- convert string to arraylist
in java - What is the difference between ArrayList.clear() and ArrayList.removeAll()?
- 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
- Fastest way to put contents of Set
to a single String with words separated by a whitespace? - What is a stack trace, and how can I use it to debug my application errors?
- How to fix “A JNI error has occurred, please check your installation.”
- Java Runtime Environment not found error when I open an application
- “PKIX path building failed” and “unable to find valid certification path to requested target”
- What exactly does a jar file contain?
- how to fix java.lang.IndexOutOfBoundsException
- error upon assigning Layout: BoxLayout can’t be shared
- Why do this() and super() have to be the first statement in a constructor?
- Else without if
- Keytool is not recognized as an internal or external command
- Cannot find “Package Explorer” view in Eclipse
- Error: Could not find or load main class in intelliJ IDE
- Java Hashmap: How to get key from value?
- PrintWriter append method not appending
- Using ADB to capture the screen
- What is the difference between response.sendRedirect() and request.getRequestDispatcher().forward(request,response)
- Recursion vs. Iteration (Fibonacci sequence)
- Java substring: ‘string index out of range’
- throw checked Exceptions from mocks with Mockito
- How to sort an array of objects in Java?
- Android – SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
- Initializing multiple variables to the same value in Java
- Java: Unresolved compilation problem
- FXML Load exception
- NullPointerExcetion Native Method Accessor… Hashing Words Issue
- How to convert currentTimeMillis to a date in Java?
- How to clear console in Java – Eclipse
- Connection Java – MySQL : Public Key Retrieval is not allowed
- Whitespace Matching Regex – Java
- Iterator for a linkedlist
- Hide Utility Class Constructor : Utility classes should not have a public or default constructor
- Android – Start service on boot
- JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object
- How to extract .war files in java? ZIP vs JAR
- What is the difference between run-time error and compiler error?
- Test if element is present using Selenium WebDriver?
- Increase heap size in Java
- Spring Boot – Cannot determine embedded database driver class for database type NONE
- Convert double to float in Java
- Can’t seem to disable Java Automatic Update