【发布时间】:2020-01-16 01:49:09
【问题描述】:
在 dart 中,Set 类有两个不同的工厂构造函数 Set.from() 和 Set.of()。
它们返回的结果是相同类型的。它们之间有什么用例区别吗?如何决定使用哪一个?
查看该程序的输出:
void main() {
List<int> myList = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5];
Set<int> mySetFrom = Set.from(myList);
Set<int> mySetOf = Set.of(myList);
print("mySetFrom type: ${mySetFrom.runtimeType}");
print("mySetFrom: ${mySetFrom}");
print("mySetOf type: ${mySetOf.runtimeType}");
print("mySetOf: ${mySetOf}");
}
输出:
mySetFrom type: _CompactLinkedHashSet<int>
mySetFrom: {1, 2, 3, 4, 5}
mySetOf type: _CompactLinkedHashSet<int>
mySetOf: {1, 2, 3, 4, 5}
【问题讨论】:
标签: dart constructor set