Constructs a new synchronized Set based on a HashSet . - Java java.util

Java examples for java.util:Set Creation

Description

Constructs a new synchronized Set based on a HashSet .

Demo Code


//package com.java2s;

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());
    }//  w  ww.ja v  a2s  .co  m

    /**
     * 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(final int initialCapacity) {
        return Collections.synchronizedSet(new HashSet<T>(initialCapacity));
    }
}

Related Tutorials