Skip to content

Commit

Permalink
Merge pull request #112 from jonas-halbach/Lesson09Changes
Browse files Browse the repository at this point in the history
added some additional explanation about maps and added an execise to …
  • Loading branch information
jonas-halbach authored Sep 24, 2023
2 parents 6252e83 + c11ceee commit 72aa489
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
20 changes: 20 additions & 0 deletions 09-ds-2-set-and-map/exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: "Exercise - Video Store"
nav_order: 0
parent: "09 - Data Structure - Set & Map"
nav_exclude: false
---

# Lesson 9: Data Structure - HashMap

1. Create a new class Movie with title, director and year.
2. Add a toString, equals and hashCode function to the class
3. Create some instances of the Movie class in the Main method, add them to a hashmap (Hashmap<Movie, Integer> where the second parameter represents the number of available copies of this movie) and test if your implementation works as expected.
4. Think about other properties of the movie class and adjust the methods mentioned in 2.
5. Extract the code from 3 into a new class Movie-Store, where a user can buy a movie, which will reduce the number of movies in the stock.
6. Build an text menu for your program where a user can buy a movie from the store.
- The program shall ask a user for his actions:
1. For list all available movies (Method signature public void printAvailableMovies())
2. Buy a movie (Method signature public void buyMovie(String title, String director, int year))
3. Quit the process
The user can execute the program when choosing option 3
34 changes: 33 additions & 1 deletion 09-ds-2-set-and-map/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ public class Main {

> As stated above, you need a `Map` when you are interested in keeping a dictionary or mapping of keys to values.
### Useful methods of a Map

- put -> adds a key and value to the map
- get -> returns the value associated with the key
- containsKey -> returns if the key exists in the map
- remove -> removes the item with the given key from the map

### Iterating over the elements of a Map

Different options:

#### By key:
```java
for (String key: map.keySet()) {
int value = map.get(key);
System.out.println("The value associated to key " + key + " is " + value);
}
```

#### By EntrySet
```java
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("The value associated to key " + entry.getKey() + " is " + entry.getValue());
}
```

## Using own objects as keys for a map or entry of a set

If you want to use your own classes as keys for a map or element of a set you have to override the `equals` and the `hashCode` method of your new class.
Please review [**this explanation**](https://www.geeksforgeeks.org/override-equalsobject-hashcode-method/){:target="_blank"} for more information.


## HashSet and HashMap

- **Ordering is not important**
Expand Down Expand Up @@ -118,4 +150,4 @@ Define a class called `Ratings`, use a Map to store the ratings' information, an

## [Set & Map Assignment](https://classroom.github.com/a/dUh1YtIJ ){:target="_blank"}

#### Follow the link, accept and download the assignment from GitHub Classroom
#### Follow the link, accept and download the assignment from GitHub Classroom

0 comments on commit 72aa489

Please sign in to comment.