hashset

Java hashset how to serialise multiple objects

痴心易碎 提交于 2019-12-04 06:56:19
问题 I asked a previous question about how I would save multiple save files to a .ser file, and I was suggested to use a hashset because they are serializable. I have tried to write a test piece of code to serialize every piece of information inside the hash set: public class testHashSet { public static void main(String[] args) throws ClassNotFoundException { testClass new1 = new testClass("Hello", "male", true); testClass new2 = new testClass("Goodbye", "female", true); testClass new3 = new

HashSets don't keep the elements unique if you mutate their identity

匆匆过客 提交于 2019-12-04 05:46:43
When working with HashSets in C#, I recently came across an annoying problem: HashSets don't guarantee unicity of the elements; they are not Sets. What they do guarantee is that when Add(T item) is called the item is not added if for any item in the set item.equals(that) is true . This holds no longer if you manipulate items already in the set. A small program that demonstrates (copypasta from my Linqpad): void Main() { HashSet<Tester> testset = new HashSet<Tester>(); testset.Add(new Tester(1)); testset.Add(new Tester(2)); foreach(Tester tester in testset){ tester.Dump(); } foreach(Tester

HashSet or Distinct to read distinct values of property in List<> of objects

本小妞迷上赌 提交于 2019-12-04 04:42:00
问题 This is in someway related to this (Getting all unique Items in a C# list) question. The above question is talking about a simple array of values though. I have an object returned from a third party web service: public class X { public Enum y {get; set;} } I have a List of these objects List<x> data; , about 100 records in total but variable. Now I want all the possible values in the list of the property y and I want to bind this do a CheckBoxList.DataSource (in case that makes a difference).

Should I dump java.util.HashSet in favor of CompactHashSet? [closed]

Deadly 提交于 2019-12-04 03:49:48
I found that there is an implementation of a Set that uses hashes (with all the useful consequences, like O(1) for contains() etc) that is claimed to be more efficient than java.util.HashSet in every aspect: http://ontopia.wordpress.com/2009/09/23/a-faster-and-more-compact-set/ http://alias-i.com/lingpipe/docs/api/com/aliasi/util/CompactHashSet.html Would it then be a good idea to quit using java.util.HashSet completely wherever I need a java.util.Set in favor of com.aliasi.util.CompactHashSet ? Let's start a little benchmark game. Benchmarks are based on benchmarks from the original article,

Select an element by index from a .NET HashSet

主宰稳场 提交于 2019-12-03 23:49:24
问题 At the moment I am using a custom class derived from HashSet . There's a point in the code when I select items under certain condition: var c = clusters.Where(x => x.Label != null && x.Label.Equals(someLabel)); It works fine and I get those elements. But is there a way that I could receive an index of that element within the collection to use with ElementAt method, instead of whole objects? It would look more or less like this: var c = select element index in collection under certain

Thread-safe HashSet with Guava Collections

放肆的年华 提交于 2019-12-03 23:32:07
Like the title says, i would like to get a thread-safe HashSet using Guava Collections. Can you help me? Thanks! Set<K> set = Collections.newSetFromMap(new ConcurrentHashMap<K, Boolean>()); This would be the right answer, Using the Sets class from Guava. Anyway the answer from @crhis was good intended. Sets.newSetFromMap(new ConcurrentHashMap<V, Boolean>()); Etienne Neveu Google Collections had a factory method named Sets.newConcurrentHashSet() for a while. Its implementation was similar to Chris's suggestion: public static <E> Set<E> newConcurrentHashSet() { return newSetFromMap(new

Internal System.Linq.Set<T> vs public System.Collections.Generic.HashSet<T>

落爺英雄遲暮 提交于 2019-12-03 22:58:59
Check out this piece of code from Linq.Enumerable class: static IEnumerable<TSource> DistinctIterator<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer) { Set<TSource> set = new Set<TSource>(comparer); foreach (TSource element in source) if (set.Add(element)) yield return element; } Why did the guys at Microsoft decided to use this internal implementation of Set and not the regular HashSet ? If it's better in any way, why not exposing it to the public? The implementation of this Set<T> is far simpler than HashSet<T> as it is only needs to add and remove elements and

The difference between 'HashSet' and 'Set' in Scala?

允我心安 提交于 2019-12-03 22:39:37
I'm very confused by Scala's HashSet and Set types as they both seem to do the same thing. What is the difference between them? Is it the same in Java? In my reference it says that HashSet is an "explicit set class" (as compared to Set ). What does that mean? Ben James Scala's mutable and immutable HashSet implementations are concrete classes which you can instantiate. For example, if you explicitly ask for a new scala.collection.immutable.HashSet , you will always get a set which is implemented by a hash trie. There are other set implementations, such as ListSet , which uses a list. Set is a

Find the Biggest number in HashSet/HashMap java

♀尐吖头ヾ 提交于 2019-12-03 19:07:18
问题 I would like to find the biggest number in HashSet and HashMap. Say I have the number [22,6763,32,42,33] in my HashSet and I want to find the largest number in my current HashSet..how would i do this? and Same thing for the HashMap as well. I hope you can help me with it. Thank you. 回答1: You can use Collections.max(Collection) to find the maximum element out of any collection. Similarly, for a HashMap, you can use the same method on its keySet() or values(), depending upon whether you want

nhibernate Iesi ISet fails to Remove()

本秂侑毒 提交于 2019-12-03 17:14:16
I have 2 class'es that are handled by NHibernate : AssetGroup , Asset The AssetGroup has a ISet _assets collection. The constructor of AssetGroup will say _assets = new HashSet<Asset>(); I have some operation to add , remove asset in AssetGroup public abstract class Entity<Tid> { public virtual Tid Id { get; protected set; } public override bool Equals(object obj) { return Equals(obj as Entity<Tid>); } public static bool IsTransient(Entity<Tid> obj) { return obj != null && Equals(obj.Id, default(Tid)); } private Type GetUnproxiedType() { return GetType(); } public virtual bool Equals(Entity