简体   繁体   English

Dart 列表中的 addAll() 与 followBy() 有什么区别?

[英]What's the difference between addAll() vs followedBy() in Dart lists?

I need to concatenate two lists in Dart and saw that there are at least two different methods to do so.我需要连接 Dart 中的两个列表,并看到至少有两种不同的方法可以这样做。 One is list1.addAll(list2) and the other is list1.followedBy(list2) .一个是list1.addAll(list2) ,另一个是list1.followedBy(list2) I wonder what's the difference?我想知道有什么区别? The documentation refers to the latter as "lazy concatenation".该文档将后者称为“惰性连接”。 Does it mean that under the hood the elements are copied in the first case, but only referenced in the latter?这是否意味着在引擎盖下元素在第一种情况下被复制,但仅在后一种情况下被引用? Or is there something more?或者还有更多的东西?

Lazy loading (in this case lazy concatenation) means that some resources are not evaluated before they are necessary.延迟加载(在这种情况下是延迟连接)意味着某些资源在需要之前不会被评估。 By contrast, eager loading (or eager concatenation) means that the resource is fully evaluated.相比之下,急切加载(或急切连接)意味着资源已被完全评估。

So, lazy concatenation means that the concatenation is known to be executed, but its final evaluation is postponed until it is needed.因此,惰性连接意味着已知要执行连接,但它的最终评估被推迟到需要它时。 Also, eager concatenation means that the whole concatenation process is fully done.此外,急切连接意味着整个连接过程已完全完成。

list1.addAll(list2) grows and mutates list1 to include the elements of list2 . list1.addAll(list2)增长并改变list1以包含list2的元素。 If list2 has m elements, then memory consumption from list1.addAll(list2) will by grow O(m).如果list2m个元素,那么来自list1.addAll(list2)的 memory 消耗将增加 O(m)。

More visually, if you have:更直观,如果你有:

list1                  list2
+---+---+---+---+      +---+---+
| o | o | o | o |      | o | o |
+-|-+-|-+-|-+-|-+      +-|-+-|-+
  |   |   |   |          |   |
  v   v   v   v          v   v
  A   B   C   D          E   F

then list1.addAll(list2) will produce:然后list1.addAll(list2)将产生:

list1                       list2
+---+---+---+---+---+---+   +---+---+
| o | o | o | o | o | o |   | o | o |
+-|-+-|-+-|-+-|-+-|-+-|-+   +-|-+-|-+
  |   |   |   |   |   |       |   |
  v   v   v   v    \   \______|__ |
  A   B   C   D     \   ______/  \|
                     \ /          |
                      |           v
                      v           F
                      E

Note that mutating list2 afterward will not affect the contents of list1 .请注意,之后list2不会影响list1的内容。

list1.followedBy(list2) returns an Iterable ( not a List ). list1.followedBy(list2)返回一个Iterable不是List )。 When you iterate over the that Iterable , you will iterate over list1 and then iterate over list2 .当您遍历该Iterable时,您将遍历list1然后遍历list2 list1.followedBy(list2) does not not mutate list1 and should not allocate a new List . list1.followedBy(list2)不会改变list1并且不应分配新的List Memory consumption from list1.addAll(list2) will grow by O(1) since no List s are grown nor created.来自list1.addAll(list2)的 Memory 消耗将增长 O(1),因为没有增长或创建List

In this case, mutating list2 afterward will affect* the Iterable returned by followedBy :在这种情况下,改变list2之后影响*由followedBy返回的Iterable

void main() {
  var list1 = [1, 2, 3];
  var list2 = [4, 5, 6];
  
  var list1Copy = [...list1];
  list1Copy.addAll(list2);
  
  print(list1Copy); // Prints: [1, 2, 3, 4, 5, 6]
   
  var concatenated = list1.followedBy(list2);
  print(concatenated.toList()); // Prints: [1, 2, 3, 4, 5, 6]

  list2.add(7);
  print(list1Copy); // Prints: [1, 2, 3, 4, 5, 6]
  print(concatenated.toList()); // Prints: [1, 2, 3, 4, 5, 6, 7]
}```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM