Issue
This is my code for list in which I am removing object from the list if it doesnt match certain condition ,but in some case where I dont have such key available then I am getting exception , such as if name element doesnt exist in one of my object its throws an err , I dont want to show err simply I want to just ignore from the list if the key element is not present , small json I can show for example can be ..
@Override
public List<Item> getcollectionfromapi(List<String> itemids) throws Exception {
List<Item> collection = itemRepository.findById(itemids);
Predicate<Item> name = item -> item.getProduct().getName() == null;
Predicate<Item> names = item -> item.getProduct().getName().length() == 0;
Predicate<Item> datas = item -> item.getDatas().equals("data") == false;
collection.removeIf(name);
collection.removeIf(names);
collection.removeIf(datas);
return collection;
}
}
SampleJson:-
[
{
"id": "1",
"datas": "data",
"product": {
"name": "Transport",
"value":"1"
}
},
{
"id": "2",
"product": {
"value":"2"
}
}
]
return only
[
{
"id": "1",
"datas": "data",
"product": {
"name": "Transport",
"value":"1"
}
}
Solution
Predicate<Item> name = item -> StringUtils.isBlank(item.getProduct().getName());
Predicate<Item> datas = item -> "data".equals(item.getDatas()) == false;
Answered By - japhy
Answer Checked By - Pedro (JavaFixing Volunteer)