java - Choosing between Stream and Collections API -


consider following example prints maximum element in list :

list<integer> list = arrays.aslist(1,4,3,9,7,4,8);            list.stream().max(comparator.naturalorder()).ifpresent(system.out::println); 

the same objective can achieved using collections.max method :

system.out.println(collections.max(list)); 

the above code not shorter cleaner read (in opinion). there similar examples come mind such use of binarysearch vs filter used in conjunction findany.

i understand stream can infinite pipeline opposed collection limited memory available jvm. criteria deciding whether use stream or collections api. there other reasons choosing stream on collections api (such performance). more generally, reason chose stream on older api can job in cleaner , shorter way?

stream api swiss army knife: allows quite complex operations combining tools effectively. on other hand if need screwdriver, standalone screwdriver more convenient. stream api includes many things (like distinct, sorted, primitive operations etc.) otherwise require write several lines , introduce intermediate variables/data structures , boring loops drawing programmer attention actual algorithm. using stream api can improve performance sequential code. example, consider old api:

class group {     private map<string, user> users;      public list<user> getusers() {         return new arraylist<>(users.values());     } } 

here want return users of group. api designer decided return list. can used outside in various ways:

list<user> users = group.getusers(); collections.sort(users); someothermethod(users.toarray(new user[users.size])); 

here it's sorted , converted array pass other method happened accept array. in other place getusers() may used this:

list<user> users = group.getusers(); for(user user : users) {     if(user.getage() < 18) {         throw new illegalstateexception("underage user in selected group!");     } } 

here want find user matched criteria. in both cases copying intermediate arraylist unnecessary. when move java 8, can replace getusers() method users():

public stream<user> users() {     return users.values().stream(); } 

and modify caller code. first one:

someothermethod(group.users().sorted().toarray(user[]::new)); 

the second one:

if(group.users().anymatch(user -> user.getage() < 18)) {     throw new illegalstateexception("underage user in selected group!"); } 

this way it's not shorter, may work faster well, because skip intermediate copying.

the other conceptual point in stream api stream code written according guidelines can parallelized adding parallel() step. of course not boost performance, helps more expected. if operation executed sequentially 0.1ms or longer, can benefit parallelization. anyways haven't seen such simple way parallel programming in java before.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -