hashset

What causes the slightly unpredictable ordering of the iterator() for the java.util.HashSet and HashMap.keySet() classes?

我们两清 提交于 2019-11-27 15:17:58
问题 Six years ago, I burned several days trying to hunt down where my perfectly deterministic framework was responding randomly. After meticulously chasing the entire framework ensuring that it was all using the same instance of Random, I then kept chasing by single stepping code. It was highly repetitive iterative self-calling code. Worse, the damn effect would only show up after a huge number of iterations were completed. And after +6 hours, I was finally at wits end when I discovered a line in

Java: Duplicate objects getting added to set?

南笙酒味 提交于 2019-11-27 14:54:47
问题 If I run the below code then the output is 2 which means that the set contains 2 elements. However I think that set should contain 1 since both the objects are equal based on hashcode() value as well as .equals() method. Seems like some obvious mistake in my understanding ? package HELLO; import java.util.HashSet; import java.util.Set; public class Test { public static void main(String[] args) throws Exception { Set<Alpha> s = new HashSet<Alpha>(); Alpha a1 = new Alpha(); Alpha a2 = new Alpha

How to implement ConcurrentHashSet in .Net

狂风中的少年 提交于 2019-11-27 13:05:57
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(IEnumerable<TElement> elements = null) { _internal = new ConcurrentDictionary<TElement, object>(); if

HashSet does not seem to realize that two objects are the same.

陌路散爱 提交于 2019-11-27 11:56:55
I'm trying to use HashSet to store objects of a class that I created, but apparently the same objects seem to have two different hashes, which is why the contains method does not realize that the object is already in the HashSet. This leads to my program running out of heap memory. I don't think I'm doing anything wrong, but I wanted a second opinion anyway. I've done similar operations before which all worked fine, which makes this particularly annoying. I'd appreciate any help. Here's my code move1 = new Move(t,s); if(move1.hashCode()==new Move(t,s).hashCode()) System.out.println("match");

How to retrieve actual item from HashSet<T>?

不问归期 提交于 2019-11-27 11:52:11
I've read this question about why it is not possible, but haven't found a solution to the problem. I would like to retrieve an item from a .NET HashSet<T> . I'm looking for a method that would have this signature: /// <summary> /// Determines if this set contains an item equal to <paramref name="item"/>, /// according to the comparison mechanism that was used when the set was created. /// The set is not changed. If the set does contain an item equal to /// <paramref name="item"/>, then the item from the set is returned. /// </summary> bool TryGetItem<T>(T item, out T foundItem); Searching the

How to use HashSet<string>.Contains() method in case -insensitive mode?

自古美人都是妖i 提交于 2019-11-27 11:51:29
问题 How to use HashSet<string>.Contains() method in case -insensitive mode? 回答1: You can create the HashSet with a custom comparer: HashSet<string> hs = new HashSet<string>(StringComparer.OrdinalIgnoreCase); hs.Add("Hello"); Console.WriteLine(hs.Contains("HeLLo")); 回答2: You need to create it with the right IEqualityComparer : HashSet<string> hashset = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase); 回答3: It's not necessary here, as other answers have demonstrated, but in other

Collection that allows only unique items in .NET?

巧了我就是萌 提交于 2019-11-27 11:24:42
问题 Is there a collection in C# that will not let you add duplicate items to it? For example, with the silly class of public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public override int GetHashCode() { return (FirstName + LastName + Address).GetHashCode(); } public override bool Equals(object obj) { Customer C = obj as Customer; return C != null && String.Equals(this.FirstName, C.FirstName) && String.Equals

Does adding a duplicate value to a HashSet/HashMap replace the previous value

此生再无相见时 提交于 2019-11-27 10:38:03
Please consider the below piece of code: HashSet hs = new HashSet(); hs.add("hi"); -- (1) hs.add("hi"); -- (2) hs.size() will give 1 as HashSet doesn't allow duplicates so only one element will be stored. I want to know if we add the duplicate element, then does it replace the previous element or it simply doesn't add it? Also, what will happen using HashMap for the same case? In the case of HashMap , it replaces the old value with the new one. In the case of HashSet , the item isn't inserted. Jimmy The first thing you need to know is that HashSet acts like a Set , which means you add your

How to Iterate over a Set/HashSet without an Iterator?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 09:59:34
How can I iterate over a Set / HashSet without the following? Iterator iter = set.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } assylias You can use an enhanced for loop : Set<String> set = new HashSet<String>(); //populate set for (String s : set) { System.out.println(s); } Or with Java 8: set.forEach(System.out::println); There are at least six additional ways to iterate over a set. The following are known to me: Method 1 // Obsolete Collection Enumeration e = new Vector(movies).elements(); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } Method

Why is HashSet<Point> so much slower than HashSet<string>?

*爱你&永不变心* 提交于 2019-11-27 09:13:07
问题 I wanted to store some pixels locations without allowing duplicates, so the first thing comes to mind is HashSet<Point> or similar classes. However this seems to be very slow compared to something like HashSet<string> . For example, this code: HashSet<Point> points = new HashSet<Point>(); using (Bitmap img = new Bitmap(1000, 1000)) { for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { points.Add(new Point(x, y)); } } } takes about 22.5 seconds. While the following