Store Primitive Types in a Collection in Java
Description
The following code shows how to store Primitive Types in a Collection.
Example
// ww w . j a va 2s .c om
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] argv) throws Exception {
Map map = new HashMap();
// Create int wrapper object
Integer refInt = new Integer(123);
// Store int in map
map.put("key", refInt);
// Get int value from map
refInt = (Integer) map.get("key");
// Get the integer value from wrapper object
int i = refInt.intValue();
}
}
The code above generates the following result.