java - how to selectively filter items in a collection -


I use the following snippet to filter the list of selected users, where the selection is a Boolean variable. There is an easy way (helpful function) to populate the selected user collection instead of writing the following links.

  list & lt; Users & gt; SelectUsers = New Arreelist & lt; Users & gt; (0); (User users: this.getUsers ()) {if (user.is selected)) {selectedUsers.add (user.getId ()); }}  

You can use the function from Google archive; However, you will still need to create an object, and if you want to manipulate the result, then you have to create another archive and pass the result, because the result of the filter will be an unbroken filtered view of the original archive is. / P>

To make this more concrete:

  list & lt; Users & gt; SelectUsers = New Arrestists & lt; Users & gt; (Iterables.filter (this.getusers (), new pricet & amp;; user & gt; () {public boolean apply (user ur) {return usr.is select ();}})); Of course, this is not really very clean (unless you make a different class for your classmate and reuse it in a bunch of places), and this In fact, the user returns the list, not their IDs ... you have to use "Transform" to get your ID, personally, what do I do to you right now. 


Comments