Tuesday, February 15, 2011

Best practices in Collections

If you do need a synchronized map, you can use the static method synchronizedMap(...) in class Collections to add synchronization to a regular map. Don't use the legacy collection classes, such as Hashtable. Example:

// A regular, unsynchronized map  
Map<Integer, String> map = new HashMap<Integer, String>();  
  
// Wrap it to add synchronization  
Map<Integer, String> synchedMap = Collections.synchronizedMap(map); 

No comments:

Post a Comment