Last active
May 4, 2021 07:03
-
-
Save davidmigloz/3c34006c53cdf28f1cadd1a985f55566 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: sets
This file contains 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
import 'package:collection/collection.dart'; | |
void main() { | |
// Mutable set | |
final mutableSet = {1, 2, 3, 3}; | |
mutableSet.forEach((e) => print(e)); // 1 2 3 | |
// Empty mutable set | |
final emptySet = <int>{}; | |
// Immutable set (copy of the original) | |
final immutableSet = Set.unmodifiable({1, 2, 3}); | |
// Immutable set view (wrap of the original) | |
final immutableSetView = UnmodifiableSetView(mutableSet); | |
// Equality | |
print({1, 2, 3} == {1, 2, 3}); // false | |
print(SetEquality().equals({1, 2, 3}, {1, 2, 3})); // true | |
// Creating a set from list | |
Set<int> set = Set.from([1, 2, 3, 3]); // {1, 2, 3} | |
} |
This file contains 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
fun main() { | |
// Mutable set | |
val mutableSet = mutableSetOf(1, 2, 3, 3) | |
mutableSet.forEach{ print(it) } // 1 2 3 | |
// Empty set | |
val emptySet = emptySet<Int>() | |
// Immutable set (copy of the original) | |
val immutableSet = setOf(1, 2, 3) | |
// Immutable set view (wrap of the original) | |
val immutableSetView = mutableSet as Set<Int> | |
// Equality | |
print(setOf(1, 2, 3) == setOf(1, 2, 3)) // true | |
// Creating a set from list | |
val set: Set<Int> = listOf(1,2,3,3).toSet() // {1, 2, 3} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment