O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Mapped Collection

HashMap map = new HashMap();
											map.put(key1, obj1);
											map.put(key2, obj2);
											map.get(key3, obj3);

In this phrase, we use a HashMap to create a mapped collection of objects. The HashMap has a put() method that accepts two parameters. The first parameter is a key value, and the second parameter is the object you want to store in the map. So, in this phrase, we are storing three objects—obj1, obj2, and obj3—indexed by keys—key1, key2, and key3, respectively. The HashMap class is one of the most commonly used Java classes. In a HashMap, the objects put into a map should all be of the same class type. So if obj1 is a String object, then obj2 and obj3 should also be String objects.

To retrieve the objects that you put into the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required