如何删除dart列表中的重复项? list.distinct()? [英] How to delete duplicates in a dart List? list.distinct()?
问题描述
如何从列表中删除重复的内容,而不必使用集合?有什么像list.distinct()吗?或list.unique()?
How do i delete duplicates from a list without fooling around with a set? Is there something like list.distinct()? or list.unique()?
void main() {
print("Hello, World!");
List<String> list = ['abc',"abc",'def'];
list.forEach((f)=>print("this is list $f"));
Set<String> set = new Set<String>.from(list);
print("this is #0 ${list[0]}");
set.forEach((f)=>print("set: $f"));
List<String> l2= new List<String>.from(set);
l2.forEach((f)=>print("This is new $f"));
}
Hello, World!
this is list abc
this is list abc
this is list def
this is #0 abc
set: abc
set: def
This is new abc
This is new def
编辑:thx的答案。
设置似乎更快!但是它丢失了项目的顺序:/
thx for the answers. Set seems to be way faster!!, but it loses the order of the items :/
推荐答案
我有一个名为 Reactive-Dart 的库,其中包含许多可组合的运算符,用于终止和非终止序列。对于你的场景,它看起来像这样:
I have a library called Reactive-Dart that contains many composable operators for terminating and non-terminating sequences. For your scenario it would look something like this:
final newList = [];
Observable
.fromList(['abc', 'abc', 'def'])
.distinct()
.observe((next) => newList.add(next), () => print(newList));
产生:
[abc, def]
我应该补充说,类似的功能。在github上检查一下,我相信你会找到一些合适的东西。
I should add that there are other libraries out there with similar features. Check around on github and I'm sure you'll find something suitable.
这篇关于如何删除dart列表中的重复项? list.distinct()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!