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 ListThe 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 addThe following collection
List
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
//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. Add (new Person (" aaa ", "111"));
PersonList. Add (new Person (" aaa ", "222"));
PersonList. Add (new Person (" CCC ", "111"));
FilterPerson (personList, "aaa", "111");
}
Private static List
//filter firstName and the parameters firstName different elements in the list
List
System. The out. Println (" firstNameList="+ firstNameList);
//to filter out the lastName and parameter lastName different elements in the list
List
System. The out. Println (" lastNameList="+ lastNameList);
return null;
}
Private static & lt; T> Predicate
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 listList
//to the list of objects in the firstName or lastName attribute is null or an empty string object filter
List
//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