hashset

Why there is no ConcurrentHashSet against ConcurrentHashMap

与世无争的帅哥 提交于 2019-11-26 15:35:58
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 which should be based on the ConcurrentHashMap ? Is there anything else that I am missing? I need to use Set in a multi-threaded environment. Also, If I want to create my own ConcurrentHashSet can I achieve it by just replacing the HashMap to ConcurrentHashMap and leaving the

Does adding a duplicate value to a HashSet/HashMap replace the previous value

牧云@^-^@ 提交于 2019-11-26 15:14:40
问题 Please consider the below piece of code: HashSet hs = new HashSet(); hs.add("hi"); -- (1) hs.add("hi"); -- (2) hs.size() will give 1 as HashSet doesn't allow duplicates so only one element will be stored. I want to know if we add the duplicate element, then does it replace the previous element or it simply doesn't add it? Also, what will happen using HashMap for the same case? 回答1: In the case of HashMap, it replaces the old value with the new one. In the case of HashSet, the item isn't

Why can&#39;t I retrieve an item from a HashSet without enumeration?

北慕城南 提交于 2019-11-26 14:40:52
问题 I'm looking for insight into the heads of HashSet designers. As far as I am aware, my question applies to both Java and C# HashSets, making me think there must be some good reason for it, though I can't think of any myself. After I have inserted an item into a HashSet, why is it impossible to retrieve that item without enumeration, hardly an efficient operation? Especially since a HashSet is explicitly built in a way which supports efficient retrieval. It would often be useful to me to have

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

依然范特西╮ 提交于 2019-11-26 12:50:16
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> s2 = new HashSet<String>(); S1 INT S2 ? Use the retainAll() method of Set : Set<String> s1; Set<String> s2; s1.retainAll(s2); // s1 now contains only elements in both sets If you want to preserve the sets, create a new set to hold the intersection: Set<String> intersection = new HashSet<String>(s1); // use the copy constructor intersection.retainAll(s2); The javadoc of retainAll()

Why does HashSet implementation in Sun Java use HashMap as its backing?

人走茶凉 提交于 2019-11-26 12:13:49
问题 Looking at the source of Java 6, HashSet<E> is actually implemented using HashMap<E,Object> , using dummy object instance on every entry of the Set. I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself. But, why is it still used? Is there any reason to use it besides making it easier to maintain the codes? 回答1: Actually, it's not just HashSet . All implementations of the Set interface in Java 6 are based on an underlying Map . This is not a requirement; it's just

How to Iterate over a Set/HashSet without an Iterator?

Deadly 提交于 2019-11-26 11:45:22
问题 How can I iterate over a Set / HashSet without the following? Iterator iter = set.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } 回答1: You can use an enhanced for loop: Set<String> set = new HashSet<String>(); //populate set for (String s : set) { System.out.println(s); } Or with Java 8: set.forEach(System.out::println); 回答2: There are at least six additional ways to iterate over a set. The following are known to me: Method 1 // Obsolete Collection Enumeration e = new

Ordering of elements in Java HashSet

好久不见. 提交于 2019-11-26 10:02:06
问题 Why do the second and third sets preserve order: Integer[] j = new Integer[]{3,4,5,6,7,8,9}; LinkedHashSet<Integer> i = new LinkedHashSet<Integer>(); Collections.addAll(i,j); System.out.println(i); HashSet<Integer> hi = new HashSet<Integer>(i); System.out.println(hi); LinkedHashSet<Integer> o = new LinkedHashSet<Integer>(hi); System.out.println(o); Here\'s the output I get: 3,4,5,6,7,8,9 3,4,5,6,7,8,9 3,4,5,6,7,8,9 回答1: The second one (just using HashSet ) is only a coincidence. From the

Java HashSet contains duplicates if contained element is modified

心不动则不痛 提交于 2019-11-26 09:03:44
问题 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

HashSet that preserves ordering

人盡茶涼 提交于 2019-11-26 08:56:27
问题 I need a HashSet that preserves insertion ordering, are there any implementations of this in the framework? 回答1: Standard .NET HashSet do not preserve the insertion order. For simple tests the insertion order may be preserved due to an accident, but it's not guaranteed and would not always work that way. To prove that it is enough to do some removals in between. See this question for more information on that: Does HashSet preserve insertion order? I have briefly implemented a HashSet which

Difference between HashSet and HashMap?

梦想与她 提交于 2019-11-26 08:54:40
问题 Apart from the fact that HashSet does not allow duplicate values, what is the difference between HashMap and HashSet ? I mean implementation wise? It\'s a little bit vague because both use hash tables to store values. 回答1: 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