-
-
Notifications
You must be signed in to change notification settings - Fork 738
Add maps concept #2593
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
Closed
Closed
Add maps concept #2593
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d47d8c9
About section and instructions updated
smcg468 b97f2f7
instructions updated and hints added
smcg468 b1dc173
Introduction and exercise implementation complete
smcg468 43cf2d4
Tests written and documentation updates made
smcg468 41da4cb
documentation updated
smcg468 c1095d5
merge conflicts
smcg468 c70a6a1
updating config.json
smcg468 b6de98b
adding arcade-high-score to config
smcg468 86e6f48
Fixing linting issues
smcg468 31cdb00
prerequisities in config file aligned with design prerequisites
smcg468 b9c97ff
Comments addressed
smcg468 db19f18
Merging main
smcg468 2b97e0e
merge conflict
smcg468 74d9152
lint errors resolved
smcg468 f3cafdb
removing _template
smcg468 33bc592
Indentation issue resolved
smcg468 51903cb
gradle wrapper added
smcg468 0a2f9da
Changing define map to getHighScores
smcg468 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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." | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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-- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
putmethod.getmethod.To me, the
keySetandvaluesmethods are OK to mention in theabout.md, but should not be introduced until a student knows how to interact with the map contents directly.