HashMap: Java Collections Framework Video Tutorial, Part 3

A tutorial on HashMap. Maps are data collections that function like lookup tables; basically you can store objects via “keys” (names, IDs, or even complex objects) and quickly retrieve them without having to look through an entire list.

After starting the video, click the maximise button to make it fullscreen so you can see the code!

Code For This Tutorial

App.java:

import java.util.HashMap;
import java.util.Map;

public class App {


	public static void main(String[] args) {

		HashMap<Integer, String> map = new HashMap<Integer, String>();
		
		map.put(5, "Five");
		map.put(8, "Eight");
		map.put(6, "Six");
		map.put(4, "Four");
		map.put(2, "Two");
		
		String text = map.get(6);
		
		System.out.println(text);
		
		for(Map.Entry<Integer, String> entry: map.entrySet()) {
			int key = entry.getKey();
			String value = entry.getValue();
			
			System.out.println(key + ": " + value);
		}
		
	}

}

Six
2: Two
4: Four
5: Five
6: Six
8: Eight

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

3 Responses to HashMap: Java Collections Framework Video Tutorial, Part 3

  1. ortega says:

    Hello, I have multiple markes at line9 from
    HashMap: Java Collections Framework Video Tutorial, Part 3.
    Eclipse not accept type HashMap as generic, where is the problem?. thank for all, i like your videos, greating from Frnace

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Collections Framework | Tagged , , , , , |