Sunday, October 26, 2014
Collections.synchronizedMap when map is used in thread
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
// HashMap is not synchronized
public class MapTest {
public static void main(String[] args){
Map<String, String> map = Collections.synchronizedMap(new HashMap<String, String>());
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
for (Map.Entry<String, String> entry:map.entrySet()){
System.out.println("key="+entry.getKey()+" value="+entry.getValue());
}
}
}
OUTPUT:
key=3 value=c
key=2 value=b
key=1 value=a
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment