We’ll 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!
In the past lessons we’ve seen how to create and use several standard Java collections: List
s, Map
s, and Set
s:
These collections are quite useful on their own! However, they can also be combined to great effect. Let’s see an example.
You can combine List
s, Map
s, and Set
s in many interesting ways to build data structures to solve problems.
You can create List
s of Map
s:
Or Set
s of List
s:
But generally, it’s more common for the top-level data structure to be a Map
: Map
s of Map
s, Map
s of List
s, and Map
s 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.
Write a method called sectionListsToMap
that, given a List
of String
s, parses it into a
Map<String, Set<String>>
as follows.
Each String
in the passed list contains a comma-separated list of names of people in a discussion section.
The first name is the section leader, and the rest are students.
Your map should map each section leader to the set of students in their section.
No section leader or student will appear twice in the data set.
For example, given the String
s "challen,student1", "ruisong4,student2, student3" and "friendly,student4, student5",
your map would have keys "challen", "ruisong4", and "friendly".
"challen" would map to a set containing "student1", "ruisong4" would map to a set containing "student2" and
"student3", and so on.
You should assert that the passed String
is not null
, but if it is not null
it will have the format
described above.
A few hints for approaching this problem.
First, consider how to use .split
and .trim
appropriately to parse the input String
.
You should get this part to work before proceeding.
Then consider when you need to create the map and each set, and how to populate them.
The imports java.util.Map
, java.util.Set
, java.util.HashMap
, and java.util.HashSet
are already provided
for you.
You should not need additional import
statements to complete this problem.
Next let’s discuss how to approach today’s homework problem.
This problem is a bit trickier, since we need to determine when to properly insert entries into our Map
, and do some String
parsing.
So let’s discuss how to get started.
Write a method called parseScript
that accepts a single String
and returns a Map<String, List<String>>
.
The passed String
contains a script consisting of lines separated by newlines, each with the following format:
Name: Line
For example, here's a simple script:
Geoffrey: What do you think of this homework problem?
Ahmed: it's a bit sus
Geoffrey: I bet they'll be able to figure it out!
Maaheen: We'll be here to help if they need it.
parseScript
parses the script and returns a map mapping each character's name to their lines in order.
So, for the script above, the map would contain three keys: "Geoffrey", "Ahmed", and "Maaheen".
The List<String>
for the key "Geoffrey" would contain the String
s "What do you think of this homework
problem!" and "I bet they'll be able to figure it out!"
The List<String>
for the key "Amhed" would contain the String
"it's a bit sus".
A few hints for approaching this problem.
You'll want to use .split
to parse the passed String
into individual lines.
You should assert that the passed String
is not null
, but if it's not, it will have the format described above,
and also not contain any blank lines.
You'll also need to use .split
to split each line into the name and their line of dialog.
You can assume that the character ":" only appears to delimit the name of the rest of the line.
The first time you encounter a character, there will not be an entry in your map for them.
So you should check for this, and create the ArrayList
when appropriate.
There may be extra whitespace around the name or the line of dialogue, so use .trim
appropriately.
The following imports are provided for you: java.util.List
, java.util.ArrayList
, java.util.Map
,
and java.util.HashMap
.
You should not need to use other imports to solve this problem.
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:
Need more practice? Head over to the practice page.