hashset

Why HashSet order always same for my program?

懵懂的女人 提交于 2019-11-27 08:40:36
问题 For some tutorial, they said: HashSet doesn’t maintain any order, the elements would be returned in any random order. But I write a test program, the result is always same. import java.util.*; public class HashSetDemo { public static void main(String[] args) { HashSet<String> hs1 = new HashSet<String>(); hs1.add("a"); hs1.add("b"); hs1.add("c"); hs1.add("d"); hs1.add(null); hs1.add(null); System.out.println(hs1); System.out.println(hs1); } } Output: [null, a, b, c, d] [null, a, b, c, d] I

Mutable objects and hashCode

≯℡__Kan透↙ 提交于 2019-11-27 07:51:41
Have the following class: public class Member { private int x; private long y; private double d; public Member(int x, long y, double d) { this.x = x; this.y = y; this.d = d; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = (int) (prime * result + y); result = (int) (prime * result + Double.doubleToLongBits(d)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Member) { Member other = (Member) obj; return other.x == x && other.y == y && Double.compare(d, other.d) ==

Why does HashSet implementation in Sun Java use HashMap as its backing?

久未见 提交于 2019-11-27 06:53:06
Looking at the source of Java 6, HashSet<E> is actually implemented using HashMap<E,Object> , using dummy object instance on every entry of the Set. I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself. But, why is it still used? Is there any reason to use it besides making it easier to maintain the codes? JXG Actually, it's not just HashSet . All implementations of the Set interface in Java 6 are based on an underlying Map . This is not a requirement; it's just the way the implementation is. You can see for yourself by checking out the documentation for the various

Hash Set and Array List performances

ぃ、小莉子 提交于 2019-11-27 03:56:25
I have implemented a method which simply loops around a set of CSV files that contain data on a number of different module. This then adds the 'moduleName' into a hashSet. (Code shown below) I have used a hashSet as it guarantees no duplicates are inserted instead of an ArrayList which would have to use the contain() method and iterate through the list to check if it is already there. I believe using the hash set has a better performance than an array list. Am I correct in stating that? Also, can somebody explain to me: How to work the performance for each data structure if used? What is the

Make HashSet<string> case-insensitive

自作多情 提交于 2019-11-27 03:42:52
问题 I have method with HashSet parameter. And I need to do case-insensitive Contains within it: public void DoSomething(HashSet<string> set, string item) { var x = set.Contains(item); ... } Is it any way to make existing HashSet case-insensitive (do not create new one)? I'm looking for solution with best perfomance. Edit Contains can be called multiple times. So IEnumerable extensions are not acceptable for me due to lower perfomance than native HashSet Contains method. Solution Since, answer to

Java Hashset.contains() produces mysterious result

为君一笑 提交于 2019-11-27 02:46:03
问题 I don't usually code in Java, but recently I started not having a choice. I might have some major misunderstanding of how to properly use HashSet. So it might be possible something I did is just plain wrong. However I'm grateful for any help, you might offer. So the actual problem: In a small program I was writing, I was generating very similar objects, which, when created, would have a very specific id (a string or in my last iteration a long ). Because each object would spawn new objects, I

What's better for creating distinct data structures: HashSet or Linq's Distinct()?

喜你入骨 提交于 2019-11-27 02:40:26
问题 I'm wondering whether I can get a consensus on which method is the better approach to creating a distinct set of elements: a C# HashSet or using IEnumerable's .Distinct() , which is a Linq function? Let's say I'm looping through query results from the DB with DataReader, and my options are to add the objects I construct to a List<SomeObject> or to a HashSet<SomeObject> With the List option, I would wind up having to do something like: myList = myList.Distinct().ToList<SomeObject>(); With the

What is the lookup time complexity of HashSet<T>(IEqualityComparer<T>)?

非 Y 不嫁゛ 提交于 2019-11-27 02:38:57
问题 In C#.NET, I like using HashSets because of their supposed O(1) time complexity for lookups. If I have a large set of data that is going to be queried, I often prefer using a HashSet to a List, since it has this time complexity. What confuses me is the constructor for the HashSet, which takes IEqualityComparer as an argument: http://msdn.microsoft.com/en-us/library/bb359100.aspx In the link above, the remarks note that the "constructor is an O(1) operation," but if this is the case, I am

Ordering of elements in Java HashSet

北战南征 提交于 2019-11-27 02:22:14
Why do the second and third sets preserve order: Integer[] j = new Integer[]{3,4,5,6,7,8,9}; LinkedHashSet<Integer> i = new LinkedHashSet<Integer>(); Collections.addAll(i,j); System.out.println(i); HashSet<Integer> hi = new HashSet<Integer>(i); System.out.println(hi); LinkedHashSet<Integer> o = new LinkedHashSet<Integer>(hi); System.out.println(o); Here's the output I get: 3,4,5,6,7,8,9 3,4,5,6,7,8,9 3,4,5,6,7,8,9 Behrang The second one (just using HashSet ) is only a coincidence. From the JavaDocs : This class implements the Set interface, backed by a hash table (actually a HashMap instance).

How do HashSets in Java work? [duplicate]

陌路散爱 提交于 2019-11-27 01:09:35
问题 Possible Duplicate: How does Java hashmap work? Can someone explain to me how HashSets in java work and why they are faster than using ArrayLists? 回答1: First, HashSet , unlike ArrayList is a Set: It cannot contain duplicates while ArrayList can - so they are built for different purposes. It also does not guarantee ordering - again, unlike a list. Second - a HashSet is built on the hash table data structure, that allows O(1) seek time for an element. Note that many times, a HashSet is slower