Create synchronized Map - Java Collection Framework

Java examples for Collection Framework:Map

Description

Create synchronized Map

Demo Code


//package com.java2s;

import java.util.Collections;
import java.util.HashMap;

import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        System.out.println(synchronizedMap());
    }/*w ww .jav a  2s  .co  m*/

    public static <K, V> Map<K, V> synchronizedMap() {
        return Collections.synchronizedMap(new HashMap<K, V>());
    }

    public static <K, V> Map<K, V> synchronizedMap(final int initialCapacity) {
        return Collections.synchronizedMap(new HashMap<K, V>(
                initialCapacity));
    }

    protected static <K, V> Map<K, V> synchronizedMap(
            final int initialCapacity, final float loadFactor) {
        return Collections.synchronizedMap(new HashMap<K, V>(
                initialCapacity, loadFactor));
    }
}

Related Tutorials