How to calculate the intersection of two sets? [duplicate]

This question already has answers here: Efficiently finding the intersection of a variable number of sets of strings (8 answers) Closed 1 year ago. Possible Duplicate: Efficiently finding the intersection of a variable number of sets of strings Say, have two Hashset, how to calculate the intersection of them? Set<String> s1 = new HashSet<String>(); Set<String> … Read more

Hashset vs Treeset

I’ve always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I’ve ever known has asked me pointedly why I would use a TreeSet. From a CS background, I don’t think it matters all that much which you use, and I don’t care to mess around with hash functions and … Read more

Why there is no ConcurrentHashSet against ConcurrentHashMap

HashSet is based on HashMap. If we look at HashSet<E> implementation, everything is been managed under HashMap<E,Object>. <E> is used as a key of HashMap. And we know that HashMap is not thread safe. That is why we have ConcurrentHashMap in Java. Based on this, I am confused that why we don’t have a ConcurrentHashSet … Read more

Difference between HashSet and HashMap?

They are entirely different constructs. A HashMap is an implementation of Map. A Map maps keys to values. The key look up occurs using the hash. On the other hand, a HashSet is an implementation of Set. A Set is designed to match the mathematical model of a set. A HashSet does use a HashMap to back its implementation, as you noted. However, it implements an entirely different interface. … Read more

HashSet vs. ArrayList

When its comes to the behavior of ArrayList and HashSet they are completely different classes. ArrayList Does not validate duplicates. get() is O(1) contains() is O(n) but you have fully control over the order of the entries. get add contains next remove(0) iterator.remove ArrayList O(1) O(1) O(n) O(1) O(1) O(1) Not thread safe and to make it thread safe you have to use Collections.synchronizedList(…) HashSet ensures there … Read more