简体   繁体   English

链式流是 x * O(N) 吗?

[英]Are chained streams x * O(N)?

Assuming the items in the sets stay the same, if I chain a stream after another, after performing some ops, are they now 2 O(N)?假设集合中的项目保持不变,如果我一个接一个地链接一个流,在执行一些操作之后,它们现在是 2 O(N) 吗?

Same with regards to a modified set, is it now O(N) + O(M)?与修改集相同,现在是 O(N) + O(M) 吗?

// o(n)
Stream.of("foo", "bar", "foobar")
    .collect(Collectors.partitioningBy(s -> s.length() > 3));

// 2O(n)? O(N) + O(M)?
Stream.of("foo", "bar", "foobar")
        .collect(Collectors.partitioningBy(s -> s.length() > 3))
        .get(true)
        .stream()
        .collect(Collectors.toMap(String::length, Function.identity()));

When measuring time complexity, the constants are dropped.在测量时间复杂度时,常量被删除。 O(2N) is the same as O(N). O(2N) 与 O(N) 相同。 In the case of streams, it can be looked at like this:在流的情况下,可以这样看:

for (Item item :items) {
  doSomething1(item);
  doSomething2(item);
}

Vs this:对比这个:

for (Item item :items) {
  doSomething1(item);
}

for (Item item :items) {
  doSomething2(item);
}

In both cases the time complexity is O(N) overall, where N is the number of items.在这两种情况下,时间复杂度总体上都是 O(N),其中 N 是项目的数量。

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

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