hashset

HashSet<T> versus Dictionary<K, V> w.r.t searching time to find if an item exists

吃可爱长大的小学妹 提交于 2019-11-26 05:20:11
问题 HashSet<T> t = new HashSet<T>(); // add 10 million items Dictionary<K, V> t = new Dictionary<K, V>(); // add 10 million items. Whose .Contains method will return quicker? Just to clarify, my requirement is I have 10 million objects (well, strings really) that I need to check if they exist in the data structure. I will NEVER iterate. 回答1: HashSet vs List vs Dictionary performance test, taken from here. Add 1000000 objects (without checking duplicates) Contains check for half the objects of a

How to initialize HashSet values by construction?

こ雲淡風輕ζ 提交于 2019-11-26 04:57:20
问题 I need to create a Set with initial values. Set<String> h = new HashSet<String>(); h.add(\"a\"); h.add(\"b\"); Is there a way to do this in one line of code? For instance, it\'s useful for a final static field. 回答1: There is a shorthand that I use that is not very time efficient, but fits on a single line: Set<String> h = new HashSet<>(Arrays.asList("a", "b")); Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set. When

Does HashSet preserve insertion order?

走远了吗. 提交于 2019-11-26 04:56:42
问题 Does the HashSet collection introduced in .NET 3.5 preserve insertion order when iterated using foreach ? The documentation states, that the collection is not sorted, but it doesn\'t say anything about insertion order. A pre-release BCL blog entry states that it is unordered, but this article states that it is designed to preserve insertion order. My limited testing suggests, that order is preserved, but that could be a coincidence. 回答1: This HashSet MSDN page specifically says: A set is a

Hashset vs Treeset

喜夏-厌秋 提交于 2019-11-25 23:35:39
问题 I\'ve always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I\'ve ever known has asked me pointedly why I would use a TreeSet . From a CS background, I don\'t think it matters all that much which you use, and I don\'t care to mess around with hash functions and buckets (in the case of Java ). In which cases should I use a HashSet over a TreeSet ? 回答1: HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add,