hashset

Why doesn't java.util.HashSet have a get(Object o) method?

我的未来我决定 提交于 2019-11-27 00:20:14
问题 I've seen other questions about getting objects from Set 's based on index value and I understand why that is not possible. But I haven't been able to find a good explanation for why a get by object is not allowed so thought I would ask. HashSet is backed by a HashMap so getting an object from it should be pretty straightforward. As it is now, it appears I would have to iterate over each item in the HashSet and test for equality which seems unnecessary. I could just use a Map but I have no

HashSet vs LinkedHashSet

回眸只為那壹抹淺笑 提交于 2019-11-26 23:29:30
What is the difference between them? I know that A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet when you care about the iteration order. When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted. But in sourcecode of LinkedHashSet there are only calling constructors of HashSet. So where is double-linked List and insertion order? The answer lies in which constructors the LinkedHashSet uses to

HashSet contains duplicate entries

点点圈 提交于 2019-11-26 23:20:26
问题 A HashSet only stores values ones, when the equals method says that they're the same. Thats what I thought. But now i'm adding Elements to a HashSet where the equals method returns true and the size of the set still grows?? sorry I'm confused. Some hints where i'm wrong would be nice. Element t1 = new Element(false, false, false, false); Element t2 = new Element(true, true, true, true); Element t3 = new Element(false, false, false, false); if (t1.equals(t3)) System.out.println("they're equal"

HashSet contains problem with custom objects

試著忘記壹切 提交于 2019-11-26 22:55:25
问题 My Custom class that will be contained by HashSet public class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "hashcode='" + this.hashCode() + '\'' + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Person)) return false; Person person = (Person) o; if (age != person.age) return false; if (

Java HashSet contains duplicates if contained element is modified

荒凉一梦 提交于 2019-11-26 22:47:06
Let's say you have a class and you create a HashSet which can store this instances of this class. If you try to add instances which are equal, only one instance is kept in the collection, and that is fine. However if you have two different instances in the HashSet, and you take one and make it an exact copy of the other (by copying the fields), the HashSet will then contain two duplicate instances. Here is the code which demonstrates this: public static void main(String[] args) { HashSet<GraphEdge> set = new HashSet<>(); GraphEdge edge1 = new GraphEdge(1, "a"); GraphEdge edge2 = new GraphEdge

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

旧街凉风 提交于 2019-11-26 22:21:19
问题 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

How to access the reference values of a HashSet<TValue> without enumeration?

大憨熊 提交于 2019-11-26 21:50:26
问题 I have this scenario in which memory conservation is paramount. I am trying to read in > 1 GB of Peptide sequences into memory and group peptide instances together that share the same sequence. I am storing the Peptide objects in a Hash so I can quickly check for duplication, but found out that you cannot access the objects in the Set, even after knowing that the Set contains that object. Memory is really important and I don't want to duplicate data if at all possible. (Otherwise I would of

When should I use the HashSet<T> type?

自闭症网瘾萝莉.ら 提交于 2019-11-26 21:26:23
I am exploring the HashSet<T> type, but I don't understand where it stands in collections. Can one use it to replace a List<T> ? I imagine the performance of a HashSet<T> to be better, but I couldn't see individual access to its elements. Is it only for enumeration? Robert Rossney The important thing about HashSet<T> is right there in the name: it's a set . The only things you can do with a single set is to establish what its members are, and to check whether an item is a member. Asking if you can retrieve a single element (e.g. set[45] ) is misunderstanding the concept of the set. There's no

How can I convert a Java HashSet<Integer> to a primitive int array?

只愿长相守 提交于 2019-11-26 20:08:38
问题 I've got a HashSet<Integer> with a bunch of Integers in it. I want to turn it into an array, but calling hashset.toArray(); returns an Object[] . Is there a better way to cast it to an array of int other than iterating through every element manually? I want to pass the array to void doSomething(int[] arr) which won't accept the Object[] array, even if I try casting it like doSomething((int[]) hashSet.toArray()); 回答1: Apache's ArrayUtils has this (it still iterates behind the scenes):

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

蓝咒 提交于 2019-11-26 19:31:44
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> ? Thanks. BonyT 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 existing in Set Can perform mathematical set operations against a Set: Union/Intersection