Skip to content

Instantly share code, notes, and snippets.

@malte0811
Created October 26, 2015 11:20
Show Gist options
  • Save malte0811/d628d12e932ec0cd256c to your computer and use it in GitHub Desktop.
Save malte0811/d628d12e932ec0cd256c to your computer and use it in GitHub Desktop.
package tmp;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class Main {
public static void main(String[] args) {
Set<String> s = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
s.add("1");
s.add("2");
s.add("3");
s.add("4");
Iterator<String> it = s.iterator();
System.out.println(it.next());
s.remove("4");
System.out.println(it.next());
System.out.println(it.next());
}
}
@malte0811
Copy link
Author

Ok, you can ignore my comment on the issue, github did not automatically subscribe me to this thread as it does with prs and issues.
I think pretty much every set(hashset and probably most others as well) is backed by a map <T, Boolean>, so it would not be additional memory.
So, are you ok with me pr'ing that commit?

@mindforger
Copy link

as you said, every set is backed by a list or map ... the one it is created from of course XD .. but the default implementation for most sets is base of a non-concurrent safe list or hashmap, therefore i was curious if your aproach is properly CME safe and you proved me wrong :D thank you for teaching me a lesson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment