Java 8 list manipulation -


i'm trying basic map/filter operations on list in listchangelistener.onchanged(change<? extends place>) method , can working using old-fashioned "iterate , ifs" way, wanted try write using stream() method java 8. commented part doesn't give same result though, fails filter out categories correctly (and yes, have working implementation of equals(object) category

for (place p : change.getaddedsublist()) {     if (!categories.contains(p.getcategory())) {         categories.add(p.getcategory());     } } // list<category> addedcategories = change.getaddedsublist().stream() //                      .map(place::getcategory) //                      .filter((c) -> { return !categories.contains(c); }) //                                       .collect(collectors.tolist()); // categories.addall(addedcategories); 

that's because in first version, once have added category list, subsequent occurrence of category isn't added second time: have added list. second version doesn't same thing. need make sure categories unique in stream:

change.getaddedsublist().stream()       .map(place::getcategory)       .distinct()       .filter(c -> !categories.contains(c))       .foreachordered(c -> categories.add(c)); 

note don't need collect temporary list.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -