Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Last active May 4, 2021 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmigloz/3c34006c53cdf28f1cadd1a985f55566 to your computer and use it in GitHub Desktop.
Save davidmigloz/3c34006c53cdf28f1cadd1a985f55566 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: sets
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}
}
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