Conditional Expressions and Statements : 01/20/2023
Operations on Variables : 01/19/2023
Variables and Types : 01/18/2023
Welcome to CS 124 : 01/17/2023
Practice with Collections
importjava.util.List;
importjava.util.Map;
importjava.util.ArrayList;
importjava.util.HashMap;
Map<String, List<String>>petNames=newHashMap<>();
petNames.put("Geoffrey", newArrayList<>());
petNames.get("Geoffrey").add("Gracie");
petNames.get("Geoffrey").add("Xyz");
System.out.println(petNames);
Let’s pause before moving on to get more practice with Java’s collections—the lists, maps, and sets that are so useful for solving problems.
We’ll also learn how we can combine these collections together to build more interesting data structures.
Let’s get started!
You can combine Lists, Maps, and Sets in many interesting ways to build data structures to solve problems.
You can create Lists of Maps:
importjava.util.List;
importjava.util.Map;
importjava.util.ArrayList;
importjava.util.HashMap;
List<Map<String, String>>values=newArrayList<>();
Map<String, String>entry=newHashMap<>();
entry.put("test", "me");
values.add(entry);
System.out.println(values);
Or Sets of Lists:
importjava.util.Set;
importjava.util.List;
importjava.util.HashSet;
importjava.util.ArrayList;
List<String>first=newArrayList<>();
first.add("test");
first.add("me");
List<String>second=newArrayList<>();
second.add("test");
second.add("me");
Set<List<String>>set=newHashSet<>();
set.add(first);
System.out.println(set);
set.add(second);
// Because first and second have the same items, adding second does not modify the set
System.out.println(set);
But generally, it’s more common for the top-level data structure to be a Map: Maps of Maps, Maps of Lists, and Maps of Sets.
We’ll get some practice working with these on this lesson’s practice and homework problems.
We’ll spend the rest of the lesson working on some problems that test our understanding of how to nest collections.
First, we’re asked to parse a List<String> into a Map<Set<String>>.
Let’s do an example of that together, which you can use as a starting point for the practice problem that follows.
Today’s homework problem is a challenge!
Your goal is to complete the implementation of the Hawaiian Word translator you began earlier this semester.
Since this problem is more difficult than other homework problems, we’re giving you an extra day to complete it.
And, at the end of the day, please remember that this is just one problem, and you have homework drops.
We’ll also get you started with a walkthrough to help you think about how to approach this problem.
Note that the problem asks you to throw an exception in certain cases, something that we have not yet covered.
The walkthrough describes how to do that.
Very few people can make a legitimate claim to the label “genius”.
Dina Katabi is one of them.
A full professor at MIT, her groundbreaking work on wireless networking and other topics has also earned her a MacArthur Fellowship, the substantial financial award unofficially known as the “Genius Grant”.
In this video she discusses some of her work, including the ability to use wireless signals is a way that you may find quite surprising: