Skip to content

Commit ebfb3ed

Browse files
Add sets concept (#2769)
1 parent 098daf2 commit ebfb3ed

File tree

16 files changed

+711
-0
lines changed

16 files changed

+711
-0
lines changed

concepts/sets/.meta/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "Sets are unordered collections that do not allow duplicates.",
3+
"authors": ["sanderploegsma"],
4+
"contributors": []
5+
}

concepts/sets/about.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# About
2+
3+
A [`Set`][set-docs] is an unordered collection that (unlike `List`) is guaranteed to not contain any duplicate values.
4+
5+
The generic type parameter of the `Set` interface denotes the type of the elements contained in the `Set`:
6+
7+
```java
8+
Set<Integer> ints = Set.of(1, 2, 3);
9+
Set<String> strings = Set.of("alpha", "beta", "gamma");
10+
Set<Object> mixed = Set.of(1, false, "foo");
11+
```
12+
13+
Note that the `Set.of()` method creates an [_unmodifiable_][unmodifiable-set-docs] `Set` instance.
14+
Trying to call methods like `add` and `remove` on this instance will result in an exception at run-time.
15+
16+
To create a modifiable `Set`, you need to instantiate a class that implements the `Set` interface.
17+
The most-used built-in class that implements this interface is the [`HashSet`][hashset-docs] class.
18+
19+
```java
20+
Set<Integer> ints = new HashSet<>();
21+
```
22+
23+
The `Set` interface extends from the [`Collection`][collection-docs] and [`Iterable`][iterable-docs] interfaces, and therefore shares a lot of methods with other types of collections.
24+
A notable difference to the `Collection` interface, however, is that methods like `add` and `remove` return a `boolean` (instead of `void`) which indicates whether the item was contained in the set when that method was called:
25+
26+
```java
27+
Set<Integer> set = new HashSet<>();
28+
set.add(1);
29+
// => true
30+
set.add(2);
31+
// => true
32+
set.add(1);
33+
// => false
34+
set.size();
35+
// => 2
36+
set.contains(1);
37+
// => true
38+
set.contains(3);
39+
// => false
40+
set.remove(3);
41+
// => false
42+
set.remove(2);
43+
// => true
44+
set.size();
45+
// => 1
46+
```
47+
48+
[collection-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collection.html
49+
[hashset-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/HashSet.html
50+
[iterable-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Iterable.html
51+
[set-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html
52+
[unmodifiable-set-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html#unmodifiable

concepts/sets/introduction.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Introduction
2+
3+
A [`Set`][set-docs] is an unordered collection that (unlike `List`) is guaranteed to not contain any duplicate values.
4+
5+
The generic type parameter of the `Set` interface denotes the type of the elements contained in the `Set`:
6+
7+
```java
8+
Set<Integer> ints = Set.of(1, 2, 3);
9+
Set<String> strings = Set.of("alpha", "beta", "gamma");
10+
Set<Object> mixed = Set.of(1, false, "foo");
11+
```
12+
13+
Note that the `Set.of()` method creates an [_unmodifiable_][unmodifiable-set-docs] `Set` instance.
14+
Trying to call methods like `add` and `remove` on this instance will result in an exception at run-time.
15+
16+
To create a modifiable `Set`, you need to instantiate a class that implements the `Set` interface.
17+
The most-used built-in class that implements this interface is the [`HashSet`][hashset-docs] class.
18+
19+
```java
20+
Set<Integer> ints = new HashSet<>();
21+
```
22+
23+
The `Set` interface extends from the [`Collection`][collection-docs] and [`Iterable`][iterable-docs] interfaces, and therefore shares a lot of methods with other types of collections.
24+
A notable difference to the `Collection` interface, however, is that methods like `add` and `remove` return a `boolean` (instead of `void`) which indicates whether the item was contained in the set when that method was called:
25+
26+
```java
27+
Set<Integer> set = new HashSet<>();
28+
set.add(1);
29+
// => true
30+
set.add(2);
31+
// => true
32+
set.add(1);
33+
// => false
34+
set.size();
35+
// => 2
36+
set.contains(1);
37+
// => true
38+
set.contains(3);
39+
// => false
40+
set.remove(3);
41+
// => false
42+
set.remove(2);
43+
// => true
44+
set.size();
45+
// => 1
46+
```
47+
48+
[collection-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collection.html
49+
[hashset-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/HashSet.html
50+
[iterable-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Iterable.html
51+
[set-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html
52+
[unmodifiable-set-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html#unmodifiable

concepts/sets/links.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html",
4+
"description": "java.util.Set API documentation"
5+
},
6+
{
7+
"url": "https://www.baeldung.com/java-set-operations",
8+
"description": "Implementing set operations in Java"
9+
}
10+
]

config.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,19 @@
295295
"prerequisites": [
296296
"numbers"
297297
]
298+
},
299+
{
300+
"slug": "gotta-snatch-em-all",
301+
"name": "gotta-snatch-em-all",
302+
"uuid": "a7938215-4597-4c51-8ceb-6514d5654485",
303+
"concepts": [
304+
"sets"
305+
],
306+
"prerequisites": [
307+
"lists",
308+
"generic-types"
309+
],
310+
"status": "beta"
298311
}
299312
],
300313
"practice": [
@@ -1890,6 +1903,11 @@
18901903
"slug": "randomness",
18911904
"name": "Randomness"
18921905
},
1906+
{
1907+
"uuid": "775572da-46f5-4d8a-b154-f35ae344ea40",
1908+
"slug": "sets",
1909+
"name": "Sets"
1910+
},
18931911
{
18941912
"uuid": "8a468b14-724a-4036-8edb-d19a02809840",
18951913
"slug": "strings",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Hints
2+
3+
## General
4+
5+
- The [`Set` API documentation][set-docs] contains a list of methods available on the `Set` interface.
6+
7+
## 1. Start a collection
8+
9+
- The [`HashSet` class][hashset-docs] has a constructor that takes a `Collection`.
10+
11+
## 2. Grow the collection
12+
13+
- Check out the signature of the [`add` method][set-add-docs] defined on the `Set` interface.
14+
15+
## 3. Start trading
16+
17+
- You can create a copy of a `Set` by wrapping it in a `new HashSet<>()`.
18+
- The [`Set` interface][set-docs] has a method that removes all items contained in another `Collection`.
19+
20+
## 4. Identify common cards
21+
22+
- You can create a copy of a `Set` by wrapping it in a `new HashSet<>()`.
23+
- The [`Set` interface][set-docs] has a method that retains all items contained in another `Collection`.
24+
25+
## 5. All of the cards
26+
27+
- You can create a copy of a `Set` by wrapping it in a `new HashSet<>()`.
28+
- The [`Set` interface][set-docs] has a method that adds all items contained in another `Collection`.
29+
30+
[hashset-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/HashSet.html
31+
[set-add-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html#add(E)
32+
[set-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Instructions
2+
3+
Your nostalgia for Blorkemon™️ cards is showing no sign of slowing down, you even started collecting them again, and you
4+
are getting your friends to join you.
5+
6+
In this exercise, you will use the `Set` interface to help you manage your collection, since duplicate cards are not
7+
important when your goal is to get all existing cards.
8+
9+
## 1. Start a collection
10+
11+
You just found your old stash of Blorkemon™️ cards! so it's time to start a new collection!
12+
The stash contains a bunch of duplicate cards, so it's time to start a new collection by removing the duplicates.
13+
14+
You really want your friends to join your Blorkemon™️ madness, and the best way is to kickstart their collection by
15+
giving them one card.
16+
17+
Implement the `newCollection` method, which transforms a list of cards into a `Set` representing your new collection.
18+
19+
```java
20+
GottaSnatchEmAll.newCollection(List.of("Newthree", "Newthree", "Newthree"));
21+
// => {"Newthree"}
22+
```
23+
24+
## 2. Grow the collection
25+
26+
Once you have a collection, it takes a life of its own and must grow.
27+
28+
Implement the `addCard` method, which takes a new card and your current set of collected cards.
29+
The method should add the new card to the collection if it isn't already present, and should return a `boolean`
30+
indicating whether the collection was updated.
31+
32+
```java
33+
Set<String> collection = GottaSnatchEmAll.newCollection("Newthree");
34+
GottaSnatchEmAll.addCard("Scientuna",collection);
35+
// => true
36+
37+
collection.contains("Scientuna");
38+
// => true
39+
```
40+
41+
## 3. Start trading
42+
43+
You really want your friends to join your Blorkemon™️ madness, so it's time to start trading!
44+
45+
Not every trade is worth doing, or can be done at all.
46+
You cannot trade a card you don't have, and you shouldn't trade a card for one that you already have.
47+
48+
Implement the `canTrade` method, that takes your current collection and the collection of one of your friends.
49+
It should return a `boolean` indicating whether a trade is possible, following the rules above.
50+
51+
```java
52+
Set<String> myCollection = Set.of("Newthree");
53+
Set<String> theirCollection = Set.of("Scientuna");
54+
GottaSnatchEmAll.canTrade(myCollection, theirCollection);
55+
// => true
56+
```
57+
58+
## 4. Identify common cards
59+
60+
You and your Blorkemon™️ enthusiast friends gather and wonder which cards are the most common.
61+
62+
Implement the `commonCards` method, which takes a list of collections and returns a collection of cards that all collections
63+
have.
64+
65+
```java
66+
GottaSnatchEmAll.commonCards(List.of(Set.of("Scientuna"), Set.of("Newthree","Scientuna")));
67+
// => {"Scientuna"}
68+
```
69+
70+
## 5. All of the cards
71+
72+
Do you and your friends collectively own all of the Blorkemon™️ cards?
73+
74+
Implement the `allCards` method, which takes a list of collections and returns a collection of all different cards in
75+
all the collections combined.
76+
77+
```java
78+
GottaSnatchEmAll.allCards(List.of(Set.of("Scientuna"), Set.of("Newthree","Scientuna")));
79+
// => {"Newthree", "Scientuna"}
80+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Introduction
2+
3+
## Sets
4+
5+
A [`Set`][set-docs] is an unordered collection that (unlike `List`) is guaranteed to not contain any duplicate values.
6+
7+
The generic type parameter of the `Set` interface denotes the type of the elements contained in the `Set`:
8+
9+
```java
10+
Set<Integer> ints = Set.of(1, 2, 3);
11+
Set<String> strings = Set.of("alpha", "beta", "gamma");
12+
Set<Object> mixed = Set.of(1, false, "foo");
13+
```
14+
15+
Note that the `Set.of()` method creates an [_unmodifiable_][unmodifiable-set-docs] `Set` instance.
16+
Trying to call methods like `add` and `remove` on this instance will result in an exception at run-time.
17+
18+
To create a modifiable `Set`, you need to instantiate a class that implements the `Set` interface.
19+
The most-used built-in class that implements this interface is the [`HashSet`][hashset-docs] class.
20+
21+
```java
22+
Set<Integer> ints = new HashSet<>();
23+
```
24+
25+
The `Set` interface extends from the [`Collection`][collection-docs] and [`Iterable`][iterable-docs] interfaces, and therefore shares a lot of methods with other types of collections.
26+
A notable difference to the `Collection` interface, however, is that methods like `add` and `remove` return a `boolean` (instead of `void`) which indicates whether the item was contained in the set when that method was called:
27+
28+
```java
29+
Set<Integer> set = new HashSet<>();
30+
set.add(1);
31+
// => true
32+
set.add(2);
33+
// => true
34+
set.add(1);
35+
// => false
36+
set.size();
37+
// => 2
38+
set.contains(1);
39+
// => true
40+
set.contains(3);
41+
// => false
42+
set.remove(3);
43+
// => false
44+
set.remove(2);
45+
// => true
46+
set.size();
47+
// => 1
48+
```
49+
50+
[collection-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collection.html
51+
[hashset-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/HashSet.html
52+
[iterable-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Iterable.html
53+
[set-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html
54+
[unmodifiable-set-docs]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Set.html#unmodifiable
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Introduction
2+
3+
%{concept:sets}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"authors": [
3+
"sanderploegsma"
4+
],
5+
"contributors": [
6+
"kahgoh"
7+
],
8+
"files": {
9+
"solution": [
10+
"src/main/java/GottaSnatchEmAll.java"
11+
],
12+
"test": [
13+
"src/test/java/GottaSnatchEmAllTest.java"
14+
],
15+
"exemplar": [
16+
".meta/src/reference/java/GottaSnatchEmAll.java"
17+
],
18+
"invalidator": [
19+
"build.gradle"
20+
]
21+
},
22+
"forked_from": [
23+
"elm/gotta-snatch-em-all"
24+
],
25+
"icon": "character-study",
26+
"blurb": "Learn to use sets by playing a vintage trading card game"
27+
}

0 commit comments

Comments
 (0)