hashset

Java HashSet key/value pair

Why does Java not provide functions to get at the key/value pairs in a HashSet like in Hashtable? It seems like a real pain to have to iterate over it every time you need to get at something. Or am I just a newb missing something? ...

How to extract N random different elements from a set of unique elements?

I have a set of unique elements (there are not two identical elements). And I would like to extract N random and different elements from the set. What is the easiest way to do it in Java? ...

How to sort HashSet() function data in sequence?

I am new to Java, the function I would like to perform is to load a series of data from a file, into my hashSet() function. the problem is, I able to enter all the data in sequence, but I can't retrieve it out in sequence base on the account name in the file. Can anyone help? below is my code: public Set retrieveHistory(){ Se...

(C#) iterate over read-only private collection member

I have a class which has two HashSet<String> collections as private members. Other classes in my code would like to be able to iterate over those HashSets and read their contents. I don't want to write a standard getter because another class could still do something like myClass.getHashSet().Clear(); Is there any other way to expose t...

Two Key HashSet?

I need a HashSet implementation where the elements are a pair of Integers eg. Set s = { {1,2} , {3,4} , {1,4}}. Here the set s has 3 elements. This kind of two key HashSet is needed in many situations like, I have a relation in my database where the candidate key is a combination of two columns. Is there some library which already pro...

How to implement IEquatable<T> when mutable fields are part of the equality - Problem with GetHashCode

Hello! I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable<T> interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal other As Address) As Boolean _ Implements System.IEquatable(Of Address).Equa...

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

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

Iteration order of HashSet

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 identic...

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

HashSet<T> t = new HashSet<T>(); // add 10 million items Dictionary<T, K> t = new Dictionary<T, K>(); // 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....

Simple Question:Output of below Java program

public class abc1 { private String s; public abc1(String s){this.s=s;} public static void main(String args[]) { HashSet<Object> hs=new HashSet<Object>(); abc1 a1= new abc1("abc"); abc1 a2= new abc1("abc"); String s1= new String("abc"); String s2= new String("abc"); hs.add(a1); hs.add(a2); hs.add(s1); hs.add(s2); ...

Duplicate elements in a hashset

I have a problem with hashsets at the moment. I have classes which are immutable and contain just one item, when I add two different classes with the same data to a hashset, I get them both in the set. This is weird, because I've overloaded Equals and GetHashCode on both the base class and the superclass. public abstract class Contact :...

In java, what is the difference between a HashSet and HashMap....?

Apart from the fact that hashSet does not allow duplicate values, what is the difference between a HashMap and Hashset...? I mean implementaion wise.....? It's a little bit vague because both use hash table to store values..... ...

Java HashSet and data type Short, incompatibility ?

Running this code: public class SomeSet { public static void main(String[] args) { Set<Short> s = new HashSet<Short>(); for (short i = 0; i < 100; i++) { s.add(i); s.remove(i - 1); } System.out.println(s.size()); } } Will print the value 100. Why does it pri...

What are the fastest-performing options for a read-only, unordered collection of unique strings?

Disclaimer: I realize the totally obvious answer to this question is HashSet<string>. It is absurdly fast, it is unordered, and its values are unique. But I'm just wondering, because HashSet<T> is a mutable class, so it has Add, Remove, etc.; and so I am not sure if the underlying data structure that makes these operations possible make...

What's the best way to validate the values of a Set in a unit test?

Okay, often I'll have a method that returns a Set of some sort. The problem with unit testing such a method is that there is no guarantee an iteration over the set will always return the items in the same order. Does anyone have any preferred method of validating a Set? Peter ...

C#: Dictionary values to hashset conversion

Please, suggest the shortest way to convert Dictionary<Key, Value> to Hashset<Value> Is there built-in ToHashset() LINQ extension for IEnumerables ? Thank you in advance! ...

[Java] Huge performance difference between Vector and HashSet

I have a program which fetches records from database (using Hibernate) and fills them in a Vector. There was an issue regarding the performance of the operation and I did a test with the Vector replaced by a HashSet. With 300000 records, the speed gain is immense - 45 mins to 2 mins! So my question is, what is causing this huge differe...

Tricky algorithm... finding multiple combinations of subsets within nested HashSets?

I have a problem where I have to find multiple combinations of subsets within nested hashsets. Basically I have a "master" nested HashSet, and from a collection of "possible" nested HashSets I have to programmatically find the "possibles" that could be simultaneous subsets of the "master". Lets say I have the following: var...

HashSet . slow performance in big set

Hi! I have encountered a problem i cannot find a solution. I am using a HashSet to store values. The values I store is of the custom type Cycles where i have overridden the HashCode and equals as following in order to make sure the slow performance is not cuased by the hascode or the equal methods Also i have set the initial capacity of...

Removing duplicate byte[]s from a collection.

This will probably be an extremely simple question. I'm simply trying to remove duplicate byte[]s from a collection. Since the default behaviour is to compare references, I tought that creating an IEqualityComparer would work, but it doesn't. I've tried using a HashSet and LINQ's Distinct(). Sample code: using System; using System.Co...