I have a List of following objects:
public class OptionDetailResponse {
private long id;
private String flavor;
private String size;
private String status;
private String barcode;
}
I want to search in a List of those objects based on all 4 fields (except id
):
flavor
(input from a combobox)size
(input from a combobox)status
(input from a combobox)barcode
(input from a textfield)
This is my UI with the 4 input fields:
What I tried
I tried to use Predicate<OptionDetailResponse>
for searching:
Predicate<OptionDetailResponse> selectFlavor = e -> e.getParentName().equals(flavor);
Predicate<OptionDetailResponse> selectSize = e -> e.getName().equals(size);
Predicate<OptionDetailResponse> selectStatus = e -> e.getStatus().equals(status);
Predicate<OptionDetailResponse> inputBarcode = e -> e.getBarcode().contains(barcode);
List<OptionDetailResponse> list = responseList.stream().filter(
selectFlavor.and(selectSize).and(selectStatus).and(inputBarcode))
.collect(Collectors.<OptionDetailResponse>toList());
But the list returned only a correct result when selected a value for in all search-fields.
Questions
- How can I have all list when all field is empty using Predicate ?
- Do have other ways to search by multiple fields ?