I wonder if it possible to make this method better, so I have this method:
public int getLabelIdByLabelName(String labelName) throws ApiException {
List<LabelInfo> labelsList = getAllLabels();
return labelsList.stream()
.filter(label -> label.getName().equals(labelName))
.findFirst()
.map(LabelInfo::getId)
.orElse(0);
}
And this is the method which is using it:
public void enableSpecificDevices(RuleIdentifier identifier, String[] labelNames) throws ApiException {
List<Integer> labelsIdList = getLabelListById(identifier);
for (String labelName : labelNames) {
labelsIdList.remove(Integer.valueOf(deviceAPI.getLabelIdByLabelName(labelName)));
}
DisableRequest disableRequest = getDisableRequestBody(deviceIdList, labelsIdList);
sendDisableEnableRequest(disableRequest, identifier);
}
This method returns int value : deviceAPI.getLabelIdByLabelName(labelName).
As you can see in the for loop i am calling getLabelIdByLabelName each time and then perform the logic I need, its resource consuming for no reason I wonder how to return the list of integers from this list which will be something like this:
getting List once looping over the array of names which will be equal to the name and adding it to a new integer list and return it.