Depends on your data;
HashSet gives you an O(1) contains() method but doesn't preserve order.
ArrayList contains() is O(n) but you can control the order of the entries.
Array if you need to insert anything in between, worst case can be O(n), since you will have to move the data down and make room for the insertion. In Set, you can directly use SortedSet which too has O(n) too but with flexible operations.
I believe Set is more flexible.
Answer from JNL on Stack OverflowDepends on your data;
HashSet gives you an O(1) contains() method but doesn't preserve order.
ArrayList contains() is O(n) but you can control the order of the entries.
Array if you need to insert anything in between, worst case can be O(n), since you will have to move the data down and make room for the insertion. In Set, you can directly use SortedSet which too has O(n) too but with flexible operations.
I believe Set is more flexible.
The choice greatly depends on what do you want to do with it.
If it is what mentioned in your question:
I have a collection of objects that are guaranteed to be distinct (in particular, indexed by a unique integer ID). I also know exactly how many of them there are
If this is what you need to do, the you need neither of them. There is a size() method in Collection for which you can get the size of it, which mean how many of them there are in the collection.
If what you mean for "collection of object" is not really a collection, and you need to choose a type of collection to store your objects for further processing, then you need to know, for different kind of collections, there are different capabilities and characteristic.
First, I believe to have a fair comparison, you should consider using ArrayList instead Array, for which you don't need to deal with the reallocation.
Then it become the choice of ArrayList vs HashSet, which is quite straight-forward:
Do you need a List or Set? They are for different purpose: Lists provide you indexed access, and iteration is in order of index. While Sets are mainly for you to keep a distinct set of data, and given its nature, you won't have indexed access.
After you made your decision of List or Set to use, then it is a choice of List/Set implementation, normally for Lists, you choose from ArrayList and LinkedList, while for Sets, you choose between HashSet and TreeSet.
All the choice depends on what you would want to do with that collection of data. They performs differently on different action.
For example, an indexed access in ArrayList is O(1), in HashSet (though not meaningful) is O(n), (just for your interest, in LinkedList is O(n), in TreeSet is O(nlogn) )
For adding new element, both ArrayList and HashSet is O(1) operation. Inserting in the middle is O(n) for ArrayList, while it doesn't make sense in HashSet. Both will suffer from reallocation, and both of them need O(n) for the reallocation (HashSet is normally slower in reallocation, because it involve calculation of hash for each element again).
To find if certain element exists in the collection, ArrayList is O(n) and HashSet is O(1).
There are still lots of operations you can do, so it is quite meaningless to discuss for performance without knowing what you want to do.
java - HashSet vs. ArrayList - Stack Overflow
Can someone help please explain hashmaps and hashsets in java to me
java - HashSet vs ArrayList
performance - Why ArrayList is faster than HashSet in java? - Stack Overflow
A Set represents a generic "set of values". A TreeSet is a set where the elements are sorted (and thus ordered), a HashSet is a set where the elements are not sorted or ordered.
A HashSet is typically a lot faster than a TreeSet.
A TreeSet is typically implemented as a red-black tree (See http://en.wikipedia.org/wiki/Red-black_tree - I've not validated the actual implementation of sun/oracle's TreeSet), whereas a HashSet uses Object.hashCode() to create an index in an array. Access time for a red-black tree is O(log(n)) whereas access time for a HashSet ranges from constant-time to the worst case (every item has the same hashCode) where you can have a linear search time O(n).
The HashSet is an implementation of a Set.
When its comes to the behavior of ArrayList and HashSet they are completely different classes.
ArrayList
ArrayListDoes 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
HashSetensures 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(...)
What data structure should I use to best implement this? Since I will be changing the property of the Student objects in set student (thereby changing the hashcodes) should I use an ArrayList instead?
If the hashcodes for the set elements are liable to change, then you should NOT be using a HashSet. (If you do, the data structure will break, and elements in the set are liable to go missing.)
But I doubt you should be using ArrayList either, because if hashcode() is sensitive to changes to the object, then equals(Object) will most likely be too. And that means that contains(...) and similar methods won't be able to find objects.
I think you should be using a Map type, and using a "student identifier" as the key.
(You could also override hashcode and equals so that equality means that two objects have the same id. But that makes equals(Object) useless for other purposes.)
I still do not understand it at all after a year of doing CS in uni. If possible please explain them both to me in an ELI5 style. Thanks
In java, what is the difference between a hash set and an arraylist, and in what kind of situation would you choose a hashset?
The set will give much better performance (O(n) vs O(n^2) for the list), and that's normal because set membership (the contains operation) is the very purpose of a set.
Contains for a HashSet is O(1) compared to O(n) for a list, therefore you should never use a list if you often need to run contains.
The ArrayList uses an array for storing the data. The ArrayList.contains will be of O(n) complexity. So essentially searching in array again and again will have O(n^2) complexity.
While HashSet uses hashing mechanism for storing the elements into their respective buckets. The operation of HashSet will be faster for long list of values. It will reach the element in O(1).
My experiment shows that HashSet is faster than an ArrayList starting at collections of 3 elements inclusively.
A complete results table
| Boost | Collection Size |
| 2x | 3 elements |
| 3x | 10 elements |
| 6x | 50 elements |
| 12x | 200 elements | <= proportion 532-12 vs 10.000-200 elements
| 532x | 10.000 elements | <= shows linear lookup growth for the ArrayList
They're completely different classes, so the question is: what kind of behaviour do you want?
HashSet ensures there are no duplicates, gives you an O(1) contains() method but doesn't preserve order.
ArrayList doesn't ensure there are no duplicates, contains() is O(n) but you can control the order of the entries.