C6: Working with Collections

CPSC 210

Learning Goals

  • Gain an overview over Java collections
  • Use the Map<K, V> type in the Java library
  • Select the appropriate collection type from the Java library

Java Collections

Collection

Map

Iterable

List

Queue

Set

HashMap

HashSet

ArrayList

LinkedList

...

closely related

Maps

  • Map<K, V> interface with two type parameters:
    • K - represents the type for the key
    • V - represents the type for the value
  • In this course we will focus on HashMap<K, V>
  • You have seen a collection of key-value pairs using a binary search tree in CPSC 110

HashMap Example

Map<String, Instructor>
Map<K, V>

values: instructor objects

keys: names

Map<String, Instructor> instructors;
instructors = new HashMap<String, Instructor>();
instructors.put("Felix", new Instructor());
instructors.put("Steve", new Instructor());
instructors.put("Meghan", new Instructor());
Instructor felix = instructors.get("Felix");
Instructor steve = instructors.get("Steve");
Instructor meghan = instructors.get("Meghan");

"Felix"

"Steve"

"Meghan"

Which Collection to Use?

Also preserves order.

Which one to use? (2)

  • Lists: for maintaining the order in which items were added
  • Sets: if there are no duplicate items in the collection
  • Maps: to represent collections of key-value pairs

HashSet

ArrayList

LinkedList

HashMap

Lecture Ticket C6

public class Section {
  private Collection<Topic> topics;

  public Section(Collection<Topic> topics) {
    this.topics = topics;
  }
}
public class Topic {
  private Text text;
  private Video video;

  public Topic(Text text, Video video) {
    this.text = text;
    this.video = video;
  }
}

Lecture Lab

We're not going to run the code today. Instead:

  1. Write down some course recommendations of the form: If you've taken Course A, you should consider taking Course B.
     
  2. Write code for a Map<Course, Set<Course>> representing those recommendations. (Create all the objects and their associations.)
    Do NOT use the existing CourseRecommender class!
     
  3. Give good reasons why we chose a Map above and why we might choose each of a Set (as above) or a List instead.

Sample call to Course constructor:
new Course("CPSC", 210)

Sample Solution

Recommendations:

  • CPSC 210: CPSC 213, CPSC 221, CPSC 310
  • CPSC 103: CPSC 107, CPSC 203
Map<Course, Set<Course>> recs = new HashMap<>();
Course c103 = new Course("CPSC",103), c107 = new Course("CPSC",107), 
  c203 = new Course("CPSC",203), c210 = new Course("CPSC",210), c213 = new Course("CPSC",213), 
  c221 = new Course("CPSC",221), c310 = new Course("CPSC",310);

// We show two ways to establish a recommendation set for learning..
// but consistency would be better in reality :)
Set<Course> courses = new HashSet<>();
courses.add(c213);
courses.add(c221);
courses.add(c310);
recs.put(c210, courses);

recs.put(c103, new HashSet<>());
recs.get(c103).add(c107);
recs.get(c103).add(c203);
  • A Map gets us easily from a taken to its recommended follow-ups
  • A Set ensures the follow-ups for a given course are unique
  • A List would let us preserve order (like for ranked recommendations).

C6: Working with Collections

The End - Thank You!

C6 Working with Collections

By Steven Wolfman

C6 Working with Collections

  • 298