Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maps concept #2593

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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--
44 changes: 44 additions & 0 deletions concepts/maps/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.

## 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.

## Adding values to a Map

The [put][put] method is what is used to add a value to a map. The first argument passed will be the key which must be unique
and the second argument will be the value. Both the key and value can be of any data type

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

hashmap.put(String key, Integer value)
```

## Retrieving values from a Map

The [get][get] method is used to retrieve values from a map. Only one argument is passed to this method which is the key of the map entry you want to retrieve.

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

hashmap.get(String key)
```

[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--
[put]: google
[get]: google
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."
}
]
120 changes: 70 additions & 50 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@
"generic-types"
],
"status": "beta"
},
{
"slug": "arcade-high-score",
"name": "Arcade High Score",
"uuid": "07893a1a-eb93-4ad2-b98e-f3946c5e7fb2",
"concepts": [
"maps"
],
"prerequisites": [
"numbers",
"strings"
]
}
],
"practice": [
Expand Down Expand Up @@ -1455,7 +1467,9 @@
"slug": "poker",
"name": "Poker",
"uuid": "57eeac00-741c-4843-907a-51f0ac5ea4cb",
"practices": [],
"practices": [
"maps"
],
"prerequisites": [
"constructors",
"if-else-statements",
Expand All @@ -1473,7 +1487,8 @@
"chars",
"if-else-statements",
"lists",
"for-loops"
"for-loops",
"maps"
],
"difficulty": 7
},
Expand Down Expand Up @@ -1922,55 +1937,60 @@
"uuid": "78f3c7b2-cb9c-4d21-8cb4-7106a188f713",
"slug": "ternary-operators",
"name": "Ternary Operators"
}
],
"key_features": [
{
"title": "Modern",
"content": "Java is a modern, fast-evolving language with releases every 6 months.",
"icon": "evolving"
},
{
"title": "Statically-typed",
"content": "Every expression has a type known at compile time.",
"icon": "statically-typed"
},
{
"title": "Multi-paradigm",
"content": "Java is primarily an object-oriented language, but has many functional features introduced in v1.8.",
"icon": "multi-paradigm"
},
{
"title": "General purpose",
"content": "Java is used for a variety of workloads like web, cloud, mobile and game applications.",
"icon": "general-purpose"
},
{
"title": "Portable",
"content": "Java was designed to be cross-platform with the slogan \"Write once, run anywhere\".",
"icon": "portable"
},
{
"title": "Garbage Collection",
"content": "Java programs perform automatic memory management for their lifecycles.",
"icon": "garbage-collected"
"uuid": "5a10c26d-1f8a-46d3-ab85-686235000f0e",
"slug": "maps",
"name": "Maps"
}
],
"tags": [
"execution_mode/compiled",
"paradigm/functional",
"paradigm/imperative",
"paradigm/object_oriented",
"platform/android",
"platform/linux",
"platform/mac",
"platform/windows",
"runtime/jvm",
"typing/static",
"used_for/artificial_intelligence",
"used_for/backends",
"used_for/cross_platform_development",
"used_for/games",
"used_for/mobile"
]
}
"key_features": [
{
"title": "Modern",
"content": "Java is a modern, fast-evolving language with releases every 6 months.",
"icon": "evolving"
},
{
"title": "Statically-typed",
"content": "Every expression has a type known at compile time.",
"icon": "statically-typed"
},
{
"title": "Multi-paradigm",
"content": "Java is primarily an object-oriented language, but has many functional features introduced in v1.8.",
"icon": "multi-paradigm"
},
{
"title": "General purpose",
"content": "Java is used for a variety of workloads like web, cloud, mobile and game applications.",
"icon": "general-purpose"
},
{
"title": "Portable",
"content": "Java was designed to be cross-platform with the slogan \"Write once, run anywhere\".",
"icon": "portable"
},
{
"title": "Garbage Collection",
"content": "Java programs perform automatic memory management for their lifecycles.",
"icon": "garbage-collected"
}
],
"tags": [
"execution_mode/compiled",
"paradigm/functional",
"paradigm/imperative",
"paradigm/object_oriented",
"platform/android",
"platform/linux",
"platform/mac",
"platform/windows",
"runtime/jvm",
"typing/static",
"used_for/artificial_intelligence",
"used_for/backends",
"used_for/cross_platform_development",
"used_for/games",
"used_for/mobile"
]
}
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. Get all Highscores

- It should return a [map][maps] with all the players and their scores.
- [Create an object][create-object] of the HashMap class using the declared `Map<String ,Integer> highScores` to initialise the map.

## 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 all 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
Loading