Skip to content

Activity 13: Collections

Provided Files

Documentation

Example Code

Model 2

(Optional) Type each line of code, one at a time, in JShell:

Set<String> names = new Set<>();
Set<String> names = new HashSet<>();

names.add("WAS")
names.add("BAL")
names.add("PHI")
names

names.contains("DEN")
names.add("DEN")
names.contains("DEN")
names.contains("den")

names.add("DEN")
names.add(123)
names.size()
names

names.remove("WAS")
names.remove("IND")
names

names.isEmpty()
names.clear()
names.size()
names.isEmpty()

Model 3

(Optional) Type each line of code, one at a time, in JShell:

Map<String, String> teams;
teams = new Map<>();
teams = new HashMap<>();
teams.isEmpty()

teams.put("MIA", "Miami Dolphins")
teams.put("MIA", "Miami")
teams.size()
teams

teams.put("ATL", "Atlanta")
teams.put("SEA", "Seattle")
teams

teams.containsKey("ATL")
teams.containsKey("DEN")
teams.containsValue("Miami")
teams.containsValue("Dolphins")

teams.get("SEA")
teams.get("IND")
teams.get(0)

teams.remove("MIA")
teams.remove("MIA")
teams

teams.keySet()
teams.values()