Java Bootcamp – Workbooks and Challenges

Quidditch – Part 4

Prerequisite: You finished the lesson: hashCode().


equal objects must share the same hashCode.

When you add an equals() method, you must also add a hashCode() method that assigns equal objects the same hashCode.

  1. HashMap uses the hashCode to find which bucket the key is in.
  2. After it finds the bucket, equals() can compare your object against the key.


Task 1

Inside, add a hashCode method that assigns equal objects the same hashCode.

NOTE: Objects.hash() doesn't work with arrays. Pass the chasers field in as a String: Arrays.toString(chasers).

Task 2: Test your code

Expected Output:

  1. getScore should return the score 0.
  2. setScore should update gryffindor's score.

Task 3: getTeam

You will retrieve the Team key that matches the String parameter.

Background information:

  1. You can run through key-value pairs as an entrySet.
  2. forEach runs through every entry.
  3. Each entry contains a key and value.

Screen Shot 2021-07-11 at 3.07.01 PM.png

  • You can also run through each key as a keySet.

Screen Shot 2021-07-11 at 6.09.22 PM.png

Task: Choose to run an entrySet or keySet through a stream pipeline. Then, choose one of the following pipelines to return the matching key:

Pipeline 1

Screen Shot 2021-07-13 at 7.48.07 PM.png

Pipeline 2

Screen Shot 2021-07-13 at 7.47.23 PM.png

There are four possible ways to approach this problem. Only do one. The solution video will cover all four implementations.


  • For entrySet, the stream is a sequence of entries.
  • For keySet, the stream is a sequence of keys.
  • For pipeline 2, collect should return a list with only one element.
  • Indexing a List is the same as ArrayList.

4. Test

From main(), fetch the object named "GRYFFINDOR". Then, print the object that it returns.

Task 4: gameCount

Create a static variable called gameCount that counts up every time there's a new Game.

Good Luck!

Feedback Summary
43 students





Written Reviews
There are no written reviews yet.