HashSet vs. ArrayList

When its comes to the behavior of ArrayList and HashSet they are completely different classes. ArrayList ArrayList Does not validate duplicates. get() is O(1) contains() is O(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 … Read more

What is the difference between set and hashset in C++ STL?

hash_set is an extension that is not part of the C++ standard. Lookups should be O(1) rather than O(log n) for set, so it will be faster in most circumstances. Another difference will be seen when you iterate through the containers. set will deliver the contents in sorted order, while hash_set will be essentially random (Thanks Lou Franco). Edit: The C++11 … Read more

In Python, when to use a Dictionary, List or Set?

A list keeps order, dict and set don’t: when you care about order, therefore, you must use list (if your choice of containers is limited to these three, of course 😉 ). dict associates each key with a value, while list and set just contain values: very different use cases, obviously. set requires items to be hashable, list doesn’t: if you have non-hashable items, therefore, you cannot use set and must instead use list. … Read more

java, get set methods

This question has been asked before, but even after reading: Java “Get” and “Set” Methods Java Get/Set method returns null And more I still don’t understand how to solve my problem. When accessing variables in a class using get methods from another class I receive the value null. How do I recieve my correct values … Read more

Add list to set?

You can’t add a list to a set because lists are mutable, meaning that you can change the contents of the list after adding it to the set. You can however add tuples to the set, because you cannot change the contents of a tuple: Edit: some explanation: The documentation defines a set as an unordered collection of … Read more

How to sort a set in python?

You can’t Just read python documentation: Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. Emphasis mine. That means you will never be able to sort … Read more

C# Set collection?

Try HashSet: The HashSet(Of T) class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order… The capacity of a HashSet(Of T) object is the number of elements that the object can hold. A HashSet(Of T) object’s capacity automatically increases as elements are added to the object. … Read more