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
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
Hi Ortega, I don’t know. Email me your source code if you like – john@caveofprogramming.com. Make sure you don’t have an ancient version of Java!
hello Squiffy; It was my fault. I named my public class HashMap. I just changed to Hashmap and all was ok. I don’t find any difference with HashTable. It is true?
Thanks and Greatings from france