hashset

HashSet vs LinkedHashSet

馋奶兔 提交于 2019-11-26 08:44:54
问题 What is the difference between them? I know that A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet when you care about the iteration order. When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted. But in sourcecode of LinkedHashSet there are only calling constructors of HashSet. So where is double

When should I use the HashSet<T> type?

蹲街弑〆低调 提交于 2019-11-26 07:56:26
问题 I am exploring the HashSet<T> type, but I don\'t understand where it stands in collections. Can one use it to replace a List<T> ? I imagine the performance of a HashSet<T> to be better, but I couldn\'t see individual access to its elements. Is it only for enumeration? 回答1: The important thing about HashSet<T> is right there in the name: it's a set . The only things you can do with a single set is to establish what its members are, and to check whether an item is a member. Asking if you can

Remove Elements from a HashSet while Iterating [duplicate]

我怕爱的太早我们不能终老 提交于 2019-11-26 07:17:32
问题 This question already has answers here : Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop (24 answers) Closed 2 years ago . So, if I try to remove elements from a Java HashSet while iterating, I get a ConcurrentModificationException . What is the best way to remove a subset of the elements from a HashSet as in the following example? Set<Integer> set = new HashSet<Integer>(); for(int i = 0; i < 10; i++) set.add(i); // Throws

Iteration order of HashSet

五迷三道 提交于 2019-11-26 06:46:44
问题 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,

Hashcode and Equals for Hashset [duplicate]

一个人想着一个人 提交于 2019-11-26 06:33:10
问题 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

is the Java HashMap keySet() iteration order consistent?

余生长醉 提交于 2019-11-26 06:32:48
问题 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

Define: What is a HashSet?

爱⌒轻易说出口 提交于 2019-11-26 06:10:09
问题 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? 回答1: 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

When does HashSet &#39;add&#39; method calls equals? [duplicate]

帅比萌擦擦* 提交于 2019-11-26 06:03:42
问题 This question already has answers here : What issues should be considered when overriding equals and hashCode in Java? (11 answers) Closed last year . 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;

Why there is no ConcurrentHashSet against ConcurrentHashMap

血红的双手。 提交于 2019-11-26 05:54:54
问题 HashSet is based on HashMap. If we look at HashSet<E> implementation, everything is been managed under HashMap<E,Object> . <E> is used as a key of HashMap . And we know that HashMap is not thread safe. That is why we have ConcurrentHashMap in Java. Based on this, I am confused that why we don\'t have a ConcurrentHashSet which should be based on the ConcurrentHashMap ? Is there anything else that I am missing? I need to use Set in a multi-threaded environment. Also, If I want to create my own

How does HashSet compare elements for equality?

雨燕双飞 提交于 2019-11-26 05:21:52
问题 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 =