英文:
Java generics method to filter and find first element in list
问题
private Feature findFeature(String name, List
for (Feature item : list) {
if (item.getName().equals(name)) {
return item;
}
}
return null;
}
我有其他10个类似的方法。签名相同,逻辑相同。唯一不同的是List参数使用不同的对象,例如List<Data> list,或者List<Option> list等。所有对象都有一个getName()方法。我想创建一个通用方法并删除所有其他方法。
如果我使用类似这样的东西,
private
我无法访问getName()方法。创建这个方法最简单的方式是什么?反射API是唯一的选择吗?
编辑:
我发现了我犯的错误。像这样工作得很好:
对于(FeatureEntity entity:data.getFeatures()){
谓词
如果(entity.getFeatureId()!= null && findInList(featureList,filter)== null){
FeatureEntity n = new FeatureEntity();
n.setId(entity.getId());
// ...
}
}
英文:
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());
// ...
}
}
答案1
得分: 5
可以创建一个通用的方法,该方法接受带有 `Predicate` 的通用列表,以获取过滤后的第一个匹配项。
private <T> T findFeature(List<T> list, Predicate<T> predicate) {
return list.stream().filter(predicate).findFirst().orElse(null);
}
然后创建所需类型的 `Predicate` 并调用该方法
Predicate<Feature> filter = f -> f.getName().equalsIgnoreCase(name);
findFeature(list, filter);
另一方面,当找不到元素时,也可以返回 `Optional<T>` 而不是 `null`:
private <T> Optional<T> findFeature(List<T> list, Predicate<T> predicate) {
return list.stream().filter(predicate).findFirst();
}
英文:
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();
}
答案2
得分: 1
你可以创建一个通用的 Named 接口,就像这样:
public interface Named {
String getName();
}
然后使用通配符来指定方法需要 Named 接口:
private <T extends Named> T findInList(String name, List<T> list)
然后你就可以使用 getName 方法了。当然,你需要修改希望使用的类以实现 Named 接口(但你提到它们已经都有 getName() 方法)。
英文:
You could create a common Named interface, like
public interface Named {
String getName();
}
Then use a wildcard to specify that the method requires that Named interface.
private <T extends Named> T findInList(String name, List<T> list)
Then you can use getName. Of course, you need to modify the classes you wish to use to implements Named (but you mentioned they all already have getName() methods).
答案3
得分: 0
首先,你需要一个接口:
public interface HasName {
String getName();
}
然后,你的 Feature 类需要实现它:
public class Feature implements HasName {
在你的类名后添加 implements 后,你需要实现 getName() 方法(让你的IDE帮助你完成这一步骤)。
接着进行如下步骤:
private <T extends HasName> T findFeature(String name, List<T> list) {
// 在这里进行你的逻辑操作,getName方法将可用。
}
英文:
first, you need an Inferface:
public interface HasName {
String getName();
}
then, your Feature class has to implement it:
public class Feature implements HasName {
After you add the implements to your class name, you need to implement the getName() method (let your IDE help you with this)
then go as follows:
private <T extends HasName> findFeature(String name, List<T> list) {
// Do your logic, getName will be availible here.
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论