Time complexity of stream filter

前端 未结 3 1201
情话喂你
情话喂你 2021-02-08 00:35

I have a code like this:

List Listings = new ArrayList<>();
Listings.add(listing1);
Listings.add(listing2);
...
...
...

Listing listing= li         


        
3条回答
  •  难免孤独
    2021-02-08 01:07

    filter itself without a terminal operation would have a zero overhead - as it does absolutely nothing; streams are driven by the terminal operation only - no terminal operation, nothing gets executed.

    Then comes the case that filter has to iterate over all elements (potentially all) of the source (lazily). So time complexity of filter will depend on the source that you Stream from; in your case List, so it would be O(n).

    But that would be the worst case. You can't predicate the average case as far as I can see for filter in general because it depends on the underlying source.

提交回复
热议问题