Skip to content

Commit

Permalink
Update task.md
Browse files Browse the repository at this point in the history
language checked
  • Loading branch information
stephen-hero authored Sep 26, 2023
1 parent 281508c commit fb12860
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions duckShopServer/DuckShopServerSetDefinition/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
`Set`s are collections of _unique_ elements.
They reflect the mathematical abstraction of `set`:
a group of objects without duplicates.
It means, if we have two sets with the same size with the same objects inside,
It means, if we have two sets of the same size with the same objects inside,
these two sets will be equal.

![Set definition](../../utils/src/main/resources/images/duck/shop/theory/set_definition.png)

<div class="hint" title="Different Set implementations">

Unlike `List`, `Set` normally does not preserve the order of items.
But similar to `List` you can use the different `Set` implementations to change this property,
e.g. you can use `LinkedHashSet` implementation, that sorts elements in the set by default.
But similar to the case of `List`, you can use different `Set` implementations to change this property:
e.g., you can use the `LinkedHashSet` implementation, which sorts elements in the set by default.

Each implementation defines not only the set of possible operations with this collection,
Each implementation defines not only the set of possible operations with this collection
but also the complexity of `this` operation.
`LinkedHashSet`, for the intents and purposes of being accessed using contains is simply
`LinkedHashSet`, for the intents and purposes of being accessed using `contains`, is simply
a hash set. So, inserting elements into the linked hash set is cheap.
</div>

In general `Set` contains _only_ elements with the _same_ type.
In some cases it is possible to store elements with different types,
but which have a _relation_ between them, see the [documentation](https://kotlinlang.org/docs/generics.html) for more details.
In some cases, it is possible to store elements with different types,
but they must have a _relation_ between them, see the [documentation](https://kotlinlang.org/docs/generics.html) for more details.

![Incorrect type in a set](../../utils/src/main/resources/images/duck/shop/theory/set_wrong_type.png)

If you have a set with objects, e.g. with your own type like `Duck`,
it is possible to override behaviour to compare element in `Set`.
By default, to compare elements inside a set the [`euqals`](https://kotlinlang.org/docs/equality.html) function is used.
If you have a set with objects, e.g., with your own type like `Duck`,
it is possible to override the behavior of comparing elements in `Set`.
By default, to compare elements inside a set, the [`euqals`](https://kotlinlang.org/docs/equality.html) function is used.
It is possible to override this function and compare items only by a part of possible properties.

<div class="hint" title="An example of comparing items in Set only by a part of possible properties">
Expand All @@ -54,13 +54,13 @@ but `DuckOverridden` will be compared _only_ by `backgroundColor`:

![Set comparison example](../../utils/src/main/resources/images/duck/shop/theory/set_equal_example.png)

Since we compare only `backgroundColor`, the set in the second case consists only from one element.
Since we compare only `backgroundColor`, the set in the second case consists only of one element.

</div>

## Initialization

To create a new set you can use special _builders_:
To create a new set, you can use special _builders_:

```kotlin
val emptySet1 = emptySet<Int>() // Builds the internal object EmptySet
Expand All @@ -69,10 +69,10 @@ val emptySet2 = setOf<Int>() // Calls emptySet()

<div class="hint" title="What is the difference between emptySet and a regular one?">

In Kotlin the `emptySet` implementation is an object under the hood.
The main reason such function exists is to save allocations (creating and deleting new variables).
Since `emptySet` returns the same _singleton_ instance every time it is called so one can
use it in allocation free manner. It allows to work your Kotlin program in a more efficient way.
In Kotlin, the `emptySet` implementation is an object under the hood.
The main reason such a function exists is to save allocations (creating and deleting new variables).
Since `emptySet` returns the same _singleton_ instance every time it is called, one can
use it in allocation freely. It allows your Kotlin program to work in a more efficient way.
</div>

```kotlin
Expand Down

0 comments on commit fb12860

Please sign in to comment.