Home > Back-end >  Stream filter in writing
Stream filter in writing

Time:12-01

Collection with stream filtering, the collection objects have firstname, lastname
Method is introduced into two String variables, firstname and lastname,
Want to by variable content filtering,
The incoming variables might be content, may be an empty string
Variable is empty do not filter in the filter, the variable is not null, only keep consistent firstname and lastname
How do you write such filter, thanks
The stream (). The filter

CodePudding user response:

Is to make the comparison between the elements in the collection, just as with the if... The else... As

 

Public static List The filter (List A list of String firstName, String lastName) {
Return the list. The stream (). The filter (STR - & gt; StringUtils. IsEmpty (STR)
| | StringUtils. Equals (STR, firstName) | | StringUtils. Equals (STR, lastName))
Collect (Collectors. ToList ());
}

CodePudding user response:

I didn't express clearly, and then add
The following collection
List PersonList=new ArrayList (a);
PersonList. Add (new Person (" aaa ", "111"));
PersonList. Add (new Person (" aaa ", "222"));
PersonList. Add (new Person (" BBB ", "111"));
Realize the filtering effect of
Private static List FilterPerson (List PersonList, String firstname, String lastname) {
//if firstname, lastname empty, not filter
//if the firstname and empty, lastname 111 filter Person (" aaa ", "222")
//if firstname preach aaa lastname empty filter Person (" BBB ", "111")
Return personList. Stream (). The filter (... ).collect(Collectors.toList());
}

CodePudding user response:

Post more than a year ago, but now just to see!
Just when the headphones, the condition judgment, I didn't write, have need of your own to change;

Public class Filter {

Public static void main (String [] args) {
List PersonList=new ArrayList<> (a);
PersonList. Add (new Person (" aaa ", "111"));
PersonList. Add (new Person (" aaa ", "222"));
PersonList. Add (new Person (" CCC ", "111"));

FilterPerson (personList, "aaa", "111");
}

Private static List FilterPerson (List PersonList, String firstName, String lastName) {
//filter firstName and the parameters firstName different elements in the list
List FirstNameList=personList. Stream (). The filter (selectByKey (Person: : getFirstName, firstName)). Collect (Collectors. ToList ());
System. The out. Println (" firstNameList="+ firstNameList);


//to filter out the lastName and parameter lastName different elements in the list
List LastNameList=personList. Stream (). The filter (selectByKey (Person: : getLastName, lastName)). Collect (Collectors. ToList ());
System. The out. Println (" lastNameList="+ lastNameList);
return null;
}

Private static & lt; T> Predicate SelectByKey (Function<? Super T,?> The function, the String name) {
Return t - & gt; Name==function. The apply (t);
}

}

@ Getter
@ Setter
@ ToString
The class Person {

Private String firstName.
Private String lastName.

Public Person (String firstName, String lastName) {
Enclosing firstName=firstName;
Enclosing the lastName=lastName;
}
}

CodePudding user response:

//generates a set list
List List=Stream of (new Person (" aaa ", null), the new Person (null, "111"), the new Person (" ", "222"), the new Person (" BBB ", ""), the new Person (" CCC", "333")). Collect (Collectors. ToList ());
//to the list of objects in the firstName or lastName attribute is null or an empty string object filter
List Notnull=list. Stream (). The filter (the person - & gt;
//firstName attribute is null interception
Person. GetFirstName ()!=null & amp; &
//the interception of the lastName attribute is null
Person. GetLastName ()!=null& &
//firstName attribute for the interception of an empty string
!" ". The equals (person getFirstName ()) & amp; &
//lastName attributes to an empty string to intercept
!" ". The equals (person getLastName ())
).collect(Collectors.toList());

CodePudding user response:

Meet the conditions or filter out always can't remember
  • Related