Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions concepts/maps/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blurb": "Maps are a data structure that holds key-value pairs. Keys and values can be of any reference data type, and the keys must be unique.",
"authors": [
"smcg468"
],
"contributors": []
}
78 changes: 78 additions & 0 deletions concepts/maps/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# About

[Maps][maps] in Java is an interface that holds data in key-value pairs.

- Keys can be of any [reference type][reference-data-types], but must be unique.
- Values can be of any reference type, they do not have to be unique.

We have three different views that can all be accessed individually

- keys,

```java
Map<String, Integer> map = new HashMap<>();

// Generates a set view of the keys in the hashmap
map.keySet();
```

- values

```java
Map<String, Integer> map = new HashMap<>();

// Generates a set view of the values in the hashmap
map.values();
```

- and key-value mappings.

````java
Map<String, Integer> map = new HashMap<>();

for (String key: map.keySet()) {
System.out.println("Key = " + key);
System.out.println("Value = " + map.get(key));
}
````

## Map declaration

Due to Map being an interface, it is recommended we create an object of a class that implements the map interface as shown in the example below that is creating an object of the HashMap class.
There are many useful [built-in functions][start-of-map-functions] that allow you to modify these map objects.

```java
Map<String, Integer> hashmap = new HashMap<>();
```

In regards to the declaration of the HashMap object, the first data type specified `String` represents the data type of the key, and `Integer` represents the data type of the value in the example above.

## Classes that implement Map

We then have the main three classes that are used which implement the Map interface, these are [HashMap][hash-map], [LinkedHashMap][linked-hash-map] and [TreeMap][tree-map].
Below are some key differences in the three classes.

### Hashmap

- Implements the Map interface.
- No guaranteed order of the elements inserted.
- Null values are allowed.

### LinkedHashMap

- Implements the Map interface.
- Elements are in the order that they have been inserted.
- Null values are allowed.

### Treemap

- Implements the Map, SortedMap and NavigableMap interfaces.
- Elements are sorted according to the natural ordering, keys will be compared using the compareTo() method by default or keys can be compared using another comparator if specified.
- Null values are **not** allowed.

[maps]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html
[reference-data-types]: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3
[hash-map]: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
[linked-hash-map]: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
[tree-map]: https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
[start-of-map-functions]: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#size--
52 changes: 52 additions & 0 deletions concepts/maps/introduction.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion this introduction into Maps should focus more on the basic operations when working with maps. Specifically, I think it should show:

  • How to create a new Map instance.
  • How to set values using the put method.
  • How to retrieve values using the get method.

To me, the keySet and values methods are OK to mention in the about.md, but should not be introduced until a student knows how to interact with the map contents directly.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Introduction

[Maps][maps] in Java is an interface that holds data in key-value pairs.

- Keys can be of any [reference type][reference-data-types], but must be unique.
- Values can be of any reference type, they do not have to be unique.

We have three different views that can all be accessed individually

- keys,

```java
Map<String, Integer> map = new HashMap<>();

// Generates a set view of the keys in the hashmap
map.keySet();
```

- values

```java
Map<String, Integer> map = new HashMap<>();

// Generates a set view of the values in the hashmap
map.values();
```

- and key-value mappings.

````java
Map<String, Integer> map = new HashMap<>();

for (String key: map.keySet()) {
System.out.println("Key = " + key);
System.out.println("Value = " + map.get(key));
}
````

## Map declaration

Due to Map being an interface, it is recommended we create an object of a class that implements the map interface as shown in the example below that is creating an object of the HashMap class.
There are many useful [built-in functions][start-of-map-functions] that allow you to modify these map objects.

```java
Map<String, Integer> hashmap = new HashMap<>();
```

In regards to the declaration of the HashMap object, the first data type specified `String` represents the data type of the key, and `Integer` represents the data type of the value in the example above.

[maps]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html
[reference-data-types]: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3
[start-of-map-functions]: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#size--
26 changes: 26 additions & 0 deletions concepts/maps/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"url": "https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html",
"description": "Detailed explanation of the map interface in Java."
},
{
"url": "https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3",
"description": "Reference types defined in Java, reference types are the data types used for the key/value pairs."
},
{
"url": "https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html",
"description": "Detailed explanation of the HashMap class in Java."
},
{
"url": "https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html",
"description": "Detailed explanation of the LinkedHashMap class in Java."
},
{
"url": "https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html",
"description": "Detailed explanation of the TreeMap class in Java."
},
{
"url": "https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#size--",
"description": "List of the built-in functions that can be utilised with map objects."
}
]
60 changes: 46 additions & 14 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@
"numbers",
"strings"
]
},
{
"slug": "arcade-high-score",
"name": "Arcade High Score",
"uuid": "07893a1a-eb93-4ad2-b98e-f3946c5e7fb2",
"concepts": [
"maps"
],
"prerequisites": [
"numbers",
"strings"
]
}
],
"practice": [
Expand Down Expand Up @@ -436,11 +448,13 @@
"name": "Word Count",
"uuid": "3603b770-87a5-4758-91f3-b4d1f9075bc1",
"practices": [
"strings"
"strings",
"maps"
],
"prerequisites": [
"for-loops",
"arrays"
"arrays",
"maps"
],
"difficulty": 5
},
Expand Down Expand Up @@ -829,11 +843,13 @@
"name": "Nucleotide Count",
"uuid": "2d80fdfc-5bd7-4b67-9fbe-8ab820d89051",
"practices": [
"strings"
"strings",
"maps"
],
"prerequisites": [
"if-else-statements",
"for-loops"
"for-loops",
"maps"
],
"difficulty": 5
},
Expand Down Expand Up @@ -987,10 +1003,12 @@
"practices": [
"if-else-statements",
"for-loops",
"enums"
"enums",
"maps"
],
"prerequisites": [
"enums"
"enums",
"maps"
],
"difficulty": 7
},
Expand Down Expand Up @@ -1032,12 +1050,14 @@
"practices": [
"arrays",
"strings",
"if-else-statements"
"if-else-statements",
"maps"
],
"prerequisites": [
"arrays",
"strings",
"if-else-statements"
"if-else-statements",
"maps"
],
"difficulty": 7
},
Expand Down Expand Up @@ -1516,13 +1536,15 @@
"uuid": "873c05de-b5f5-4c5b-97f0-d1ede8a82832",
"practices": [
"for-loops",
"numbers"
"numbers",
"maps"
],
"prerequisites": [
"exceptions",
"for-loops",
"if-else-statements",
"numbers"
"numbers",
"maps"
],
"difficulty": 8
},
Expand Down Expand Up @@ -1689,10 +1711,12 @@
"name": "Parallel Letter Frequency",
"uuid": "38a405e8-619d-400f-b53c-2f06461fdf9d",
"practices": [
"strings"
"strings",
"maps"
],
"prerequisites": [
"strings"
"strings",
"maps"
],
"difficulty": 6
},
Expand Down Expand Up @@ -1981,13 +2005,16 @@
"slug": "sgf-parsing",
"name": "SGF Parsing",
"uuid": "0d6325d1-c0a3-456e-9a92-cea0559e82ed",
"practices": [],
"practices": [
"maps"
],
"prerequisites": [
"strings",
"chars",
"if-else-statements",
"lists",
"for-loops"
"for-loops",
"maps"
],
"difficulty": 7
},
Expand Down Expand Up @@ -2191,6 +2218,11 @@
"uuid": "78f3c7b2-cb9c-4d21-8cb4-7106a188f713",
"slug": "ternary-operators",
"name": "Ternary Operators"
},
{
"uuid": "5a10c26d-1f8a-46d3-ab85-686235000f0e",
"slug": "maps",
"name": "Maps"
}
],
"key_features": [
Expand Down
41 changes: 41 additions & 0 deletions exercises/concept/arcade-high-score/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Hints

## General

- A [map][maps] is an associative data structure of key-value pairs.

## 1. Define a new high score map

- It should return an empty [map][maps].
- [Create an object][create-object] of the HashMap class using the declared `Map<String ,Integer> highScores`.

## 2. Add players to the high score map

- The resulting map should be returned.
- One of the [built-in functions][map-put] can be used to put a value in a map under a given key.

## 3. Remove players from the score map

- The resulting map should be returned.
- One of the [built-in functions][map-remove] can be used to remove a key/value pair from a map.

## 4. Reset a player's score

- The resulting map should be returned with the player's score reset to an initial value of 0.
- One of the [built-in functions][map-put] can be used to put a value in a map under a given key.

## 5. Update a player's score

- The resulting map should be returned with the player's updated score.
- One of the [built-in functions][map-get-or-default] can be used to get a value in a map under a given key if the key is present and update the value, or add the key with a default value if it is not present..

## 6. Get a list of players

- One of the [built-in functions][map-keys] returns a set of all keys in a map.

[maps]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html
[create-object]: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
[map-put]: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#put-K-V-
[map-remove]: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#remove-java.lang.Object-
[map-get-or-default]: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#getOrDefault-java.lang.Object-V-
[map-keys]: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#keySet--
Loading