Doubt in API stream, filter, functions arrow

0

Good afternoon community I am new to java 8 and I have a couple of doubts when developing my code using the API stream and arrow functions, the code is as follows

List<Account> listAccounts = myList
if(fiterName.compareTo("")!=0){
      ListAccounts = ip.stream().filter(x -> filterName.equals(x.getNombre()))
                        .collect(Collectors.toList())
}

With this code I apply a filter by Name to the list of type Account my doubt comes from applying several filters that is to say besides filterName I can have filterType, filterModal, filterStatus ...

The fastest solution I find is for each filter to pass the list through a new stream "But according to me this would be the worst option since I think I would iterate once for each filter and it would not be the right thing" (take into account that if the filter is "" you do not have to do anything "

Now the solution that I see is better is to put the if inside the arrow function and apply several conditions within the statement of the function in this way

  List<Account> ListAccounts = ip.stream()
                 .filter(x -> {
                       if (fiterName.compareTo("")!=0){
                             filterName.equals(x.getNombre())
                       }
                       if (fiterType.compareTo("")!=0){
                             filterType.equals(x.getType())
                       }
                       \ Y de esta forma un statement de if por cada condicion
                       else{}
                  })
                 .collect(Collectors.toList())

The questions are:

This form is the most optimal way to do it ?, How do I tell the arrow function that in case it does not enter any if it does not do anything? (I marked as necessary the else), is there a better solution to do it? taking into account that the filters can arrive empty and then nothing should be done but they can also have value and must be applied all at the same time.

    
asked by Polo 25.07.2018 в 01:05
source

0 answers