提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
基于哈希集构造新的同步集
/** from 时代Java - N o w J a v a . c o m**/ //package com.nowjava; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] argv) { System.out.println(synchronizedSet()); } /** * Constructs a new synchronized {@code Set} based on a {@link HashSet}. * * @return a synchronized Set */ public static <T> Set<T> synchronizedSet() { return Collections.synchronizedSet(new HashSet<T>()); } /** * Constructs a new synchronized {@code Set} based on a {@link HashSet} with * the specified {@code initialCapacity}. * * @param initialCapacity * the initial capacity of the set * * @return a synchronized Set */ public static <T> Set<T> synchronizedSet(