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.





















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet