Chris Garsonn :
I have a list of GroupOffices
List<GroupOffice> officesOfTheGroup;
I want to search whether the id is not equal to groupOffice.getId (Long value) but the label is groupOffice.getLabel (String) and assign it to a boolean. The below code is working good, but is there any better approach instead of go through all items with for loop ?
public GroupOfficeDto saveGroupOffice(GroupOfficeDto groupOfficeDto) {
List<GroupOffice> officesOfTheGroup = //some list from db..
for (GroupOffice go : officesOfTheGroup) {
if ((!go.getId().equals(groupOfficeDto.getId())) && go.getLabel().equals(groupOfficeDto.getLabel())) {
return groupOfficeDto;
}
return null:
}
Or how can I use stream ? If so, is using stream is better approach for it ?
Note: I am using Java8
Ruslan :
If you need boolean value you can do:
boolean b = officesOfTheGroup.stream()
.anyMatch(office -> !office.getId().equals(5) && office.getLabel().equals("London"))