Quick summary

  • consists of key-value pairs
  • HashMaps, TreeMap, and LinkedList
  • class name: HashMap<Integer, String>

Eclipse example: hashmaps


HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(5, "five");
map.put(6, "six");
map.put(2, "two");
map.put(1, "one");
map.put(9, "nine");

Map methods

Here’s how you add to the map:

map.put(key, value);
map.put(5, "five")

Sponsored content

Here’s how you get a specific value from the map:


You pass in the key to the get method.

Then you could print out a specific key from the map like this:

String gimmesomething = map.get(4);

Iterate through the map

Here’s how you iterate through this same map:

// Here’s how you iterate through the map.

for (Map.Entry<Integer,String> entry:map.entrySet()) {
int key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":"+ value);

You use a form loop here. Then you use the getKey method and getValue method to pull the specific key and values from the map.

Other types of maps

There are a few other types of maps that are part of the Java Collections Framework. There is a TreeMap and a LinkedHashmap. Both of these other types of maps give you more control about how the items are sorted and stored in the map. So if it’s important for you to retrieve the map’s items in a specific order, look into using one of these other types of maps.


Here’s an example from Just Java 2:

import java.util.*;
public class example2  {
// the Map!
Map<String, String> phonebook = new HashMap<String, String>();

// constructor
public example2(String n[], String nums[]) {
for(int i=0; i< n.length; i++)
phonebook.put( n[i], nums[i] );

public static void main(String[] args) {
// data
String [] names = { "Lefty", "Guarav", "Wong", "Rupamay" };
String [] extns = { "4873", "4810", "3769", "0" };

// get an instance of this class
example2 ex = new example2( names, extns );

// dump out the map
System.out.println("map:  " + ex.phonebook);

// get the mappings
Set<Map.Entry<String,String>> s = ex.phonebook.entrySet();

// iterate over the mappings
//   for (Iterator i = s.iterator(); i.hasNext(); ) {
for (Map.Entry me : s) {
Object ok = me.getKey();
Object ov = me.getValue();
System.out.print("key=" + ok );
System.out.println(", value=" + ov );