Files
cs-109-intro-to-programming/src/JavaMapAndDictionary.java
2025-08-29 17:54:13 -06:00

36 lines
1.3 KiB
Java

import java.util.*;
public class JavaMapAndDictionary {
public static void main(String[] args) {
// Map Example
double[] mapGPA = new double[] { 4.0, 3.0, 2.0, 1.0, 0.0 };
char[] mapLetter = new char[] { 'A', 'B', 'C', 'D', 'F' };
Map<Character, Double> map = new HashMap<Character, Double>();
for (int i = 0; i < mapGPA.length; i++) {
map.put(mapLetter[i], mapGPA[i]); // Insert key/value into map
}
for (Character key : map.keySet()) {
System.out.println(key + " " + map.get(key));
}
// END: Map Example
System.out.println();
// Dictionary Example
double[] dictGPA = new double[] { 4.0, 3.0, 2.0, 1.0, 0.0 };
char[] dictLetter = new char[] { 'A', 'B', 'C', 'D', 'F' };
Dictionary<Character, Double> dictionary = new Hashtable<Character, Double>();
for (int i = 0; i < dictGPA.length; i++) {
dictionary.put(dictLetter[i], dictGPA[i]); // Insert key/value into map
}
for (Enumeration<Character> keys = dictionary.keys(); keys.hasMoreElements(); ) {
char keyVal = keys.nextElement();
System.out.println(keyVal + " " + dictionary.get(keyVal));
}
// END: Dictionary Example
}
}