java - Time complexity of stream filter -


i have code this:

list<listing> listings = new arraylist<>(); listings.add(listing1); listings.add(listing2); ... ... ...  listing listing= listings.stream()                 .filter(l -> l.getvin() == 456)                 .findfirst(); 

my question time complexity of filter process? if o(n), intuition convert hashset data structures time complexity become o(1), there elegant way with streams?

it o(n). stream filtering uses iteration internally.

you convert map follows:

map<integer, listing > mapofvintolisting = listings.stream().collect(collectors.tomap(listing::getvin, functions.identity()); // assuming vin unique per listing mapofvintolisting.get(456);// o(1) 

but, conversion process o(n). so, if need once, use filter. if need query same list many times, converting map may make sense.

you might try using parallel streams. in cases may more performant, depends lot on exact circumstances.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -