Constructs a new synchronized Set based on a HashSet - Java Collection Framework

Java examples for Collection Framework:HashSet

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());
    }/*from   w  w  w  .j  a  v a2s  . c o  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