Overview


A lookup map is one of the most practical tools in the toolbox. And it's easy to use.

As usual, it's example time.

Simplest Example
As we can see in the example, lookup maps are really two trick ponies:

  • put -- put an object into the lookup map at a specific key
  • get -- lookup an object from the lookup map at a specific key
private void demo1()  {
	Map<String, String> map = new HashMap<>();
	map.put("MN", "Minnesota");
	map.put("WI", "Wisconsin");
	map.put("NY", "New York");
	map.put("WA", "Washington");
	String key, name;
	key = "WI";
	name = map.get("WI");
	System.out.printf("Abbrev is %s, name is %s%n", key, name);
}
Lookup Map Using Proper Objects
In the previous example we looked up String values. In reality we'll look up a real objects like a Student, House, City, etc.

Here is an example of a student lookup.

  • String is the type of the key
  • Student is the type of the element we lookup in the map (referred to as the value)
  • Put the Student object into the map at the key
  • Get the Student object from the map for the given key


private void demoStudentLookup()  {


	List<Student> students = getAllStudents();


	Map<
#1
String,
#2
Student> map = new HashMap<>(); for (Student eachStudent: students)
#3
map.put(eachStudent.getName(), eachStudent); Student student; String key = "Foo Johnson"; student =
#4
map.get(key); }