- package cn.outofmemory.snippets.core;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.Set;
- public class SynchronizedSetExample {
- public static void main(String[] args) {
- /*
- Please note that the same API applies to any type of
- Set implementation class e.g. TreeSet etc
- */
- // Create a HashSet and populate it with elements
- HashSet hashSet = new HashSet();
- hashSet.add("element_1");
- hashSet.add("element_2");
- hashSet.add("element_3");
- // static void synchronizedSet(Set set) method returns a synchronized Set from HashSet
- Set syncSet = Collections.synchronizedSet(hashSet);
- System.out.println("syncSet contains : " + syncSet);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/151020136435.html
来源: http://www.codesnippet.cn/detail/151020136435.html