hashset

Java HashSet with a custom equality criteria? [duplicate]

女生的网名这么多〃 提交于 2019-12-17 18:45:15
问题 This question already has answers here : Is it possible in java make something like Comparator but for implementing custom equals() and hashCode() (7 answers) Closed 6 years ago . I was looking for something akin to the Java TreeSet's ability to receive a custom comparator at instantiation time, so I needed not to use the object's default equality (and hash code) criteria. The closest I could come up with was to wrap my objects in a private custom class, but that seems hacky :( This ends up

How to create a HashSet<List<Int>> with distinct elements?

不问归期 提交于 2019-12-17 16:39:47
问题 I have a HashSet that contains multiple lists of integers - i.e. HashSet<List<int>> In order to maintain uniqueness I am currently having to do two things: 1. Manually loop though existing lists, looking for duplicates using SequenceEquals . 2. Sorting the individual lists so that SequenceEquals works currently. Is there a better way to do this? Is there an existing IEqualityComparer that I can provide to the HashSet so that HashSet.Add() can automatically handle uniqueness? var hashSet = new

What is the time and space complexity of method retainAll when used on HashSets in Java?

主宰稳场 提交于 2019-12-17 16:34:07
问题 For example in the code below: public int commonTwo(String[] a, String[] b) { Set common = new HashSet<String>(Arrays.asList(a)); common.retainAll(new HashSet<String>(Arrays.asList(b))); return common.size(); } 回答1: Lets take a peruse at the code. The method retainAll is inherited from AbstractCollection and (at least in OpenJDK) looks like this: public boolean retainAll(Collection<?> c) { boolean modified = false; Iterator<E> it = iterator(); while (it.hasNext()) { if (!c.contains(it.next())

Null Object in HashSet implementation

梦想与她 提交于 2019-12-17 16:21:15
问题 In the Java API, the implementation of HashSet is using an Object as a value for the inside HashMap, // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); public boolean add(E e) { return map.put(e, PRESENT)==null; } but HashMap allows its value is null. I think that's not necessary to fill the value, so why is this needed? 回答1: Because the HashSet contract specifies that remove() return true if the specified object existed and was

How to use Comparer for a HashSet

时光毁灭记忆、已成空白 提交于 2019-12-17 13:55:35
问题 As a result of another question I asked here I want to use a HashSet for my objects I will create objects containing a string and a reference to its owner. public class Synonym { private string name; private Stock owner; public Stock(string NameSynonym, Stock stock) { name=NameSynonym; owner=stock } // [+ 'get' for 'name' and 'owner'] } I understand I need a comparer , but never used it before. Should I create a separate class? like: public class SynonymComparer : IComparer<Synonym> { public

How to retrieve actual item from HashSet<T>?

99封情书 提交于 2019-12-17 10:33:23
问题 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

How to retrieve actual item from HashSet<T>?

匆匆过客 提交于 2019-12-17 10:33:03
问题 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

Mutable objects and hashCode

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 09:33:39
问题 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

Hash Set and Array List performances

孤街浪徒 提交于 2019-12-17 07:16:39
问题 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,

What is the difference between HashSet<T> and List<T>?

我们两清 提交于 2019-12-17 04:44:53
问题 Can you explain what is the difference between HashSet<T> and List<T> in .NET? Maybe you can explain with an example in what cases HashSet<T> should be preferred against List<T> ? 回答1: Unlike a List<> ... A HashSet is a List with no duplicate members. Because a HashSet is constrained to contain only unique entries, the internal structure is optimised for searching (compared with a list) - it is considerably faster Adding to a HashSet returns a boolean - false if addition fails due to already