hashset

Concurrent add on non threadsafe HashSet - what is the worst that could happen?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 07:15:20
问题 Situation: Multiple Threads are only adding values to a non threadsafe java.util.HashSet and no other operation is done on the Set until these threads have been stopped. Question: What is the worst that could happen? 回答1: That depends on what you consider as being "worst". I'm not sure whether this question aimed at a detailed, technical analysis of the current implementation considering all possible race conditions and the nitty-gritty details of the Java memory model. So if the question is:

What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

我们两清 提交于 2019-12-18 07:08:27
问题 I just understand that LinkedHashSet does not allows duplicate elements when it is inserting. But, I dont understand how does Hashset works in Hava? I know a bit that Hashtable is used in Hashset so the hashtable used to store the elements and here also does not allow the duplicate elements. Then, Treeset is also similar to Hashset it also does not allows duplicate entries so unique elements will be seen and it follows ascending order. I have one more doubt regarding HashMap - Hashmap does

HashSet with two equals object?

人走茶凉 提交于 2019-12-18 06:55:01
问题 I created an object HashSet, and the value is an object (Triple) which is my own class. But I get a strange thing, when there are two equal objects on my HashSet, is it possible? Here is my overriding method for the equals in the class Triple @Override public boolean equals(Object other){ if (other == null) return false; if (other == this) return true; if (this.getClass() != other.getClass()) return false; Triple otherTriple = (Triple)other; if(otherTriple.getSubject().equals(getSubject()) &&

Changing the elements in a set changes the 'equals' semantics

♀尐吖头ヾ 提交于 2019-12-18 05:12:22
问题 Imagine that we have this piece of code. public class HashAddAfter { private class A { public int value; public A(int value) { this.value = value; } public void setValue(int value) { this.value = value; } // Code for hashCode()... // Code for equals()... } Set<A> list1 = new HashSet<A>(); Set<A> list2 = new HashSet<A>(); public static void main(String[] args) { HashAddAfter x = new HashAddAfter(); A e1 = x.new A(1); A e2 = x.new A(1); x.list1.add(e1); x.list2.add(e2); System.out.println(x

HashSet vs. ArrayList

[亡魂溺海] 提交于 2019-12-18 04:44:17
问题 So I have a custom class Class that will have a set of another custom class Students. So it will look something like this: public class Class { private Set<Student> students; // other methods } Now I will be adding and removing many students to the set students and i will also be changing many of the private fields of a student already in the set of students. QUESTION: What data structure should I use to best implement this? Since I will be changing the property of the Student objects in set

C++ how to insert array into hash set?

隐身守侯 提交于 2019-12-18 04:38:08
问题 I need to insert a 1D array into the hashset. But I got error while compiling. #include <stdio.h> #include <stdlib.h> #include <hash_set.h> using namespace std; int hash_comp(const int* state1,const int* state2) { int result = 0; for (i = 0; i < 16; i++) { if (state1[i] != state2[i]) { result = -1; } } return result; } struct eqArray { bool operator()(const int* a1,const int* a2) const { return hash_comp(a1,a2) == 0; } }; hash_set<int*,hash<int*>,eqArray> closelist; int main(int argc, char**

ordering a hashset example?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 02:23:32
问题 I need an example on how to use a comparable class on a HashSet to get an ascending order. Let’s say I have a HashSet like this one: HashSet<String> hs = new HashSet<String>(); How can I get hs to be in ascending order? 回答1: Use a TreeSet instead. It has a constructor taking a Comparator. It will automatically sort the Set . If you want to convert a HashSet to a TreeSet , then do so: Set<YourObject> hashSet = getItSomehow(); Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator

The HashSet<T>.removeAll method is surprisingly slow

和自甴很熟 提交于 2019-12-17 22:06:37
问题 Jon Skeet recently raised an interesting programming topic on his blog: "There's a hole in my abstraction, dear Liza, dear Liza" (emphasis added): I have a set – a HashSet , in fact. I want to remove some items from it… and many of the items may well not exist. In fact, in our test case, none of the items in the "removals" collection will be in the original set. This sounds – and indeed is – extremely easy to code. After all, we’ve got Set<T>.removeAll to help us, right? We specify the size

Adding arrays with same values to HashSet results in duplicate items

核能气质少年 提交于 2019-12-17 20:07:38
问题 I'm trying to create a set of arrays of ints, the thing is that if I try to do: HashSet<int[]> s = new HashSet<int[]>(); int a1[] = {1,2,3}; int a2[] = {1,2,3}; s.add(a1); s.add(a2) System.out.println(s.size()); Then s has two objects, but there should be only one. Note: it doesn't matter if it is HashSet< Integer[]>. It just doesn't work. Now If I try to do this with an ArrayList< Integer>, something like: HashSet<ArrayList<Integer>> s = new HashSet<ArrayList<Integer>>(); ArrayList<Integer>

Telling HashSet how to sort the data

北慕城南 提交于 2019-12-17 19:44:41
问题 I'm trying to create a HashSet (or any collection type - but I think HashSet will suit me best) that will remain in order no matter what is inserted. It's for a contact manager project I am working on. I've been experimenting, with the example below. import java.util.*; public class TestDriver{ public static void main(String[] args) { FullName person1 = new FullName("Stephen", "Harper"); FullName person2 = new FullName("Jason", "Kenney"); FullName person3 = new FullName("Peter", "MacKay");