HashSet vs ArrayList contains performance

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.

Why can’t I preallocate a hashset

Answer below was written in 2011. It’s now in .NET 4.7.2 and .NET Core 2.0; it will be in .NET Standard 2.1. There’s no technical reason why this shouldn’t be possible – Microsoft just hasn’t chosen to expose a constructor with an initial capacity. If you can call a constructor which takes an IEnumerable<T> and … Read more

Should a HashSet be allowed to be added to itself in Java?

Others have already pointed out why it is questionable from a mathematical point of view, by referring to Russell’s paradox. This does not answer your question on a technical level, though. So let’s dissect this: First, once more the relevant part from the JavaDoc of the Set interface: Note: Great care must be exercised if … Read more

Would a Java HashSet’s contains() method test equality of the strings or object identity?

It uses equals() to compare the data. Below is from the javadoc for Set adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). The equals() method for String does a character by character comparison. From the javadoc for String The result is … Read more

HashSet that preserves ordering

Standard .NET HashSet do not preserve the insertion order. For simple tests the insertion order may be preserved due to an accident, but it’s not guaranteed and would not always work that way. To prove that it is enough to do some removals in between. See this question for more information on that: Does HashSet … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)