如何删除 Dart 列表中的重复项?list.distinct()? [英] How can I delete duplicates in a Dart List? list.distinct()?

查看:22
本文介绍了如何删除 Dart 列表中的重复项?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


设置似乎更快了!!但它失去了项目的顺序:/


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆