Time complexity of stream filter

前端 未结 3 1195
情话喂你
情话喂你 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 00:57

    The worst case is O(n) but since Stream is lazy, if the value is found before, it'll stop the iteration. If you need constant time look up, all the time, converting to a Map is a good idea, at the cost of additional space; if the list if huge, you should consider that aspect. In fact, if the list is small, the difference between a Map and a List will be barely noticeable, unless you're working in a time-critical system.

提交回复
热议问题