拡張可能なコレクションクラス(ループ時間による挿入操作)

3042 ワード

Collections.synchronizedMap: 
ConcurrentHashMap: , 
import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;



/**

 *  

 */

public class AccessingMap {



    private static void userMap(final Map<String, Integer> scores) {

        scores.put("Joes", 90);

        scores.put("Mike", 85);

        

        try {

            for(final String key : scores.keySet()) {

                System.out.println(key + " " + scores.get(key));

                scores.put("Tom", 99);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    

    public static void main(String[] args) {

        System.out.println("Using HashMap");

        userMap(new HashMap<String, Integer>());//

        

        System.out.println("Using synchronized HashMap");

        userMap(Collections.synchronizedMap(new HashMap<String, Integer>()));// 

        

        System.out.println("Using ConcurrentHashMap");

        userMap(new ConcurrentHashMap<String, Integer>());//

    }

}