Maps

Data Types and Exceptions in Java

Jim White

Java Developer

Maps are lookup tables

  • Maps serve as lookup table data structures
    • Store a "value" at a specific "key"
    • Use the "key" to retrieve the stored "value"
  • Example: calling a doctor
    • They lookup a health record (the value object) given a name/birth date (the key)

Maps serve as a lookup data structure like when calling a doctor's office and they ask you for your name and date of birth to find your health record

Data Types and Exceptions in Java

Map interface

  • Map interface defines operations on the key and value pairs
    • How to store or "put" a value into the store at a specific key
    • How to "remove" the value at the key from the Map
    • How to retrieve the value given a key
  • Several implementations of Map that are similar in behavior
    • A popular implementations of Map is HashMap
1 See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Map.html for details
Data Types and Exceptions in Java

HashMap keys and values

Map or Hashmap keys and values can be the same or different types

Data Types and Exceptions in Java

HashMap construction

HashMap<Integer, String> map = new HashMap<Integer, String>();
  • The parameterized/ generic constructor for a HashMap requires two types

    • The first type = the key type

      The first generic parameter for HashMap is for the keys

    • The second type = the value type

      The second generic parameter for Hashmap is for the values

Map key and value types in the generics specify the data type for keys and values

  • Requires import of java.util.HashMap
Data Types and Exceptions in Java

HashMap methods

  • Use .put(key,value) to add a key/value pairs to the table
  • Use .remove(key) to remove the key/value pair specified at the key
  • Use .get(key) to retrieve the value from the table at the key
import java.util.HashMap
...
HashMap<Integer, String> map
    = new HashMap<Integer, String>();
map.put(0, "Jim");
map.put(1, "James");
map.put(3, null);
map.put(4, "James");
map.remove(0);
System.out.println(map);
{1=James, 3=null, 4=James}
String nickname = map.get(1);
System.out.println(nickname);
James
Data Types and Exceptions in Java

Collections

  • java.util.Collections is a supporting class
  • Used to aid in sorting, filling, copying, searching, and more
    • Only has static methods
1 See https://docs.oracle.com/en/java/javase//21/docs/api/java.base/java/util/Collections.html
Data Types and Exceptions in Java

Collection methods

  • Add many objects to a List
    • addAll(List list, Object a, b, ...)
  • Return a count of an object in a collection
    • frequency(Collection c, Object o)
  • Reorder the objects in a List
    • reverse(List list)
  • Sort the objects in a List
    • sort(List list)
  • Replace all the objects in a List with another object
    • fill(List a, Object o)
ArrayList<String> x = new ArrayList<String>();
Collections.addAll(x,
    "milk", "bread", "eggs", "milk");
System.out.println(x);

int cnt = Collections.frequency(x, "milk"); System.out.println(cnt);
Collections.reverse(x); System.out.println(x);
Collections.sort(x); System.out.println(x);
Collections.fill(x, "sugar"); System.out.println(x);
[milk, bread, eggs, milk]

2
[milk, eggs, bread, milk]
[bread, eggs, milk, milk]
[sugar, sugar, sugar, sugar]
Data Types and Exceptions in Java

Arrays

  • Sometimes need to convert an array (e.g., int[]) to a List
    • Java arrays are not resizable and we may need to grow/shrink the elements
    • List has greater capability (searching, sorting, etc.)
  • java.util.Arrays is another supporting class
    • Allows converting a Java array to a List
Data Types and Exceptions in Java

Arrays example

String[] arrayCountries = {"France", "Japan", "Brazil", "Egypt", "China"};
List<String> countries = Arrays.asList(arrayCountries);
System.out.println(countries);
[France, Japan, Brazil, Egypt, China]
Data Types and Exceptions in Java

Let's practice!

Data Types and Exceptions in Java

Preparing Video For Download...