private Feature findFeature(String name, List<Feature> list) {
for (Feature item : list) {
if (item.getName().equals(name)) {
return item;
}
}
return null;
}
I have 10 other methods like this. Same signature, same logic. Only different is that List parameter is using different objects, e.g. List<Data> list, or List<Option> list, etc. All objects has a getName() method in it. I want to create a generic method and remove all others.
If I use something like this,
private <T> T findInList(String name, List<T> list) {
I don't have access the getName() method. What is the easiest way to create this method? Is reflection api the only option?
EDIT:
I found the mistake I did. It is working just fine like this:
for (FeatureEntity entity : data.getFeatures()) {
Predicate<Feature> filter = f -> f.getName().equalsIgnoreCase(entity.getName());
if (entity.getFeatureId() != null && findInList(featureList, filter) == null) {
FeatureEntity n = new FeatureEntity();
n.setId(entity.getId());
// ...
}
}