Java generics method to filter and find first element in list

Yonetmen :
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());
        // ...
    }
}
Deadpool :

You can create generic method that takes generic list with Predicate to get the first matching item after filtering

private <T> T findFeature(List<T> list, Predicate<T> predicate) {
    return list.stream().filter(predicate).findFirst().orElse(null);
}

And then create Predicate with required type and call that method

Predicate< Feature > filter = f->f.getName().equalsIgnoreCase(name);
findFeature(list,filter);

On another side, you can also return Optional<T> instead of null when no element is found:

private <T> Optional<T> findFeature(List<T> list, Predicate<T> predicate) {
    return list.stream().filter(predicate).findFirst();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291070&siteId=1