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

查看:58
本文介绍了如何删除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天全站免登陆