java使用同步集合,java同步集合,package cn.opackage 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); }}
输出:syncSet contains : [element_1, element_2, element_3]