Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 13

Map

A map (in Python - dictionary) is an object that stores associations between keys and values (key/value pairs). The keys are like indexes. In Map, the keys are integers, strings or any type of objects. A map cannot contain duplicate keys. Each key maps to one value. A key and its corresponding value form an entry stored in a map. Maps enable fast retrieval, deletion, and updating of pairs through keys.

One of the most useful map classes is HashMap.

The following example simulates a telephone book:

Map<String, Integer> telephoneBook = new HashMap<>(); 
telephoneBook.put("Peeter Peet", 5562356); 
telephoneBook.put("Mari Maasikas", 53438956); 
System.out.println("Mari's number is " + telephoneBook.get("Mari Maasikas")); 

Note: a map has two generic types in the angle brackets: one for the key (String) and one for its value (Integer).

Also note that get returns the null value if the map contains no mapping for the key.

Session 13
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment