hashset

Iteration order of HashSet

本秂侑毒 提交于 2019-11-26 19:03:29
If every object added to a java.util.HashSet implements Object.equals() and Object.hashCode() in a deterministic fashion, is the iteration order over the HashSet guaranteed to be identical for every identical set of elements added, irrespective of the order in which they were added? Bonus question: what if the insertion order is identical as well? (Assuming Sun JDK6 with same HashSet initialization.) Edit: My original question was not clear. It is not about the general contract of HashSet, but what Sun's implementation of HashSet in JDK6 offers as guarantees concerning determinism. Is it

is the Java HashMap keySet() iteration order consistent?

梦想的初衷 提交于 2019-11-26 18:48:42
I understand that the Set returned from a Map's keySet() method does not guarantee any particular order. My question is, does it guarantee the same order over multiple iterations. For example Map<K,V> map = getMap(); for( K k : map.keySet() ) { } ... for( K k : map.keySet() ) { } In the above code, assuming that the map is not modified, will the iteration over the keySets be in the same order. Using Sun's jdk15 it does iterate in the same order, but before I depend on this behavior, I'd like to know if all JDKs will do the same. EDIT I see from the answers that I cannot depend on it. Too bad.

Hashcode and Equals for Hashset [duplicate]

匆匆过客 提交于 2019-11-26 18:40:33
This question already has an answer here: When does HashSet 'add' method calls equals? [duplicate] 4 answers Please clarify my doubt in Hashset. Consider the following code, class Person { String name; Person(String n) { name=n; } public String getName() { return name; } @Override public boolean equals(Object arg0) { System.out.println("in equals"); Person obj=(Person)arg0; System.out.println("1st "+getName()); System.out.println("2nd "+obj.getName()); if(this.getName().equals(obj.getName())) { return true; } return false; } @Override public int hashCode() { System.out.println("in hash code");

Define: What is a HashSet?

a 夏天 提交于 2019-11-26 18:00:28
HashSet The C# HashSet data structure was introduced in the .NET Framework 3.5. A full list of the implemented members can be found at the HashSet MSDN page. Where is it used? Why would you want to use it? kamaci A HashSet holds a set of objects, but in a way that it allows you to easily and quickly determine whether an object is already in the set or not. It does so by internally managing an array and storing the object using an index which is calculated from the hashcode of the object. Take a look here HashSet is an unordered collection containing unique elements. It has the standard

Does HashSet preserve insertion order?

我们两清 提交于 2019-11-26 17:58:39
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. This HashSet MSDN page specifically says: A set is a collection that contains no duplicate elements, and whose elements are in no particular order. I think the

How does HashSet compare elements for equality?

╄→гoц情女王★ 提交于 2019-11-26 17:17:48
I have a class that is IComparable : public class a : IComparable { public int Id { get; set; } public string Name { get; set; } public a(int id) { this.Id = id; } public int CompareTo(object obj) { return this.Id.CompareTo(((a)obj).Id); } } When I add a list of object of this class to a hash set: a a1 = new a(1); a a2 = new a(2); HashSet<a> ha = new HashSet<a>(); ha.add(a1); ha.add(a2); ha.add(a1); Everything is fine and ha.count is 2 , but: a a1 = new a(1); a a2 = new a(2); HashSet<a> ha = new HashSet<a>(); ha.add(a1); ha.add(a2); ha.add(new a(1)); Now ha.count is 3 . Why doesn't HashSet

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

喜夏-厌秋 提交于 2019-11-26 17:14:20
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. had HashSet vs List vs Dictionary performance test, taken from here . Add 1000000 objects (without checking duplicates) Contains check for half the objects of a collection of 10000 Remove half the objects of a collection of 10000 I assume you mean Dictionary<TKey,

How to initialize HashSet values by construction?

妖精的绣舞 提交于 2019-11-26 16:52: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. 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 initializing static final sets I usually write it like this: public static final String[] SET_VALUES = new

When does HashSet 'add' method calls equals? [duplicate]

女生的网名这么多〃 提交于 2019-11-26 16:43:37
This question already has an answer here: What issues should be considered when overriding equals and hashCode in Java? 11 answers I did this test in a HashSet comparision and equals is not being called I would like to consider equals when farAway=false (A function to check two point distances) Full compilable code, you could test it, and tells why equals is not being called in this example. public class TestClass{ static class Posicion { private int x; private int y; @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return

How to implement ConcurrentHashSet in .Net

烈酒焚心 提交于 2019-11-26 16:13:21
问题 I am trying to implement a ConcurrentHashSet in the spirit of ConcurrentDictionary, approach taken is to use a internal backing ConcurrentDictionary and write small delegating methods, this is how far i got, but well the set theoretic methods are I am stuck on, esp. I am not sure if I can use a foreach and still not violate concurrency public class ConcurrentHashSet<TElement> : ISet<TElement> { private readonly ConcurrentDictionary<TElement, object> _internal; public ConcurrentHashSet