Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字段名/类型作为参数传递给Java中的方法_Java_Oop_Design Patterns_Java 8 - Fatal编程技术网

将字段名/类型作为参数传递给Java中的方法

将字段名/类型作为参数传递给Java中的方法,java,oop,design-patterns,java-8,Java,Oop,Design Patterns,Java 8,我有一份本地化的礼物清单 public class LocalizedAttribute<T> { T value; Locale locale; } 我将其添加到类A的对象中 A.setLocalizedAttributes(ImmutableList.of( new LocalizedAttribute(bookRelatedInfo1, new Locale("US")), new LocalizedAttrib

我有一份本地化的礼物清单

public class LocalizedAttribute<T> {
    T value;
    Locale locale;
}
我将其添加到类A的对象中

A.setLocalizedAttributes(ImmutableList.of(
            new LocalizedAttribute(bookRelatedInfo1, new Locale("US")),
            new LocalizedAttribute(bookRelatedInfo2, new Locale("DE")),
            new LocalizedAttribute(bookRelatedInfo3, new Locale("JP"))
))
现在,我想分别提取本地化标题列表和摘要

getLocalizedTitles(List<LocalizedAttribute> localizedAttributes) {
    return localizedAttributes.stream()
        .map(localizedAttribute -> {
            Locale locale = localizedAttribute.getLocale();
            B b = (B) localizedAttribute.getValue();
            return new LocalizedAttribute(b.getTitle(), locale);
        })
        .collect(Collectors.toList());
}
getLocalizedTitles(列出localizedAttributes){
返回localizedAttributes.stream()
.map(localizedAttribute->{
Locale=localizedAttribute.getLocale();
B=(B)localizedAttribute.getValue();
返回新的LocalizedAttribute(b.getTitle(),locale);
})
.collect(Collectors.toList());
}

现在,如果我想获得摘要列表,我需要再次编写完全相同的方法,除了
b.getTitle
等等。有更干净的方法吗?

您可以将
函数传递给该方法:

<T> List<LocalizedAttribute<T>> getLocalizedAttributes(List<LocalizedAttribute<B>> localizedAttributes, Function<B,T> mapper) {
    return localizedAttributes.stream()
        .map(localizedAttribute -> {
            Locale locale = localizedAttribute.getLocale();
            B b = localizedAttribute.getValue();
            return new LocalizedAttribute<T>(mapper.apply(b), locale);
        })
        .collect(Collectors.toList());
}
列出getLocalizedAttributes(列出localizedAttributes,函数映射器){
返回localizedAttributes.stream()
.map(localizedAttribute->{
Locale=localizedAttribute.getLocale();
B=localizedAttribute.getValue();
返回新的LocalizedAttribute(mapper.apply(b),locale);
})
.collect(Collectors.toList());
}
可简化为:

<T> List<LocalizedAttribute<T>> getLocalizedAttributes(List<LocalizedAttribute<B>> localizedAttributes, Function<B,T> mapper) {
    return localizedAttributes.stream()
        .map(la -> new LocalizedAttribute<T>(mapper.apply(la.getValue()), la.getLocale()))
        .collect(Collectors.toList());
}
列出getLocalizedAttributes(列出localizedAttributes,函数映射器){
返回localizedAttributes.stream()
.map(la->newLocalizedAttribute(mapper.apply(la.getValue()),la.getLocale())
.collect(Collectors.toList());
}
然后,您可以将其称为:

List<LocalizedAttribute<String>> titleList = getLocalizedAttributes(list,B::getTitle);
List titleList=getLocalizedAttributes(List,B::getTitle);

List summaryList=getLocalizedAttributes(List,B::getSummary);

@Eran你说得对。错过了。你能给这个方法传递另一个参数,并根据这个参数决定你想要的是标题还是标题吗summary@pvpkiran是的,我可以有一堆if语句。但是弦在哪里?(哪个类)。@user1692342请立即尝试。函数没有错误。但是B::getTitle抛出一个错误,表示无法将字符串转换为T。@user1692342我为
getLocalizedTitles
的返回类型错误。现在更改它。@user1692342如果您传递给方法a
List
(而不是原始
List
),您不必将
getValue()
强制转换为
B
。(假设
getValue()
的返回类型是
T
而不是
Object
)。如果将
getLocalizedAttributes()
返回的值存储在
列表中而不是
列表中,编译器不知道传递的函数应该是
函数。您不应该使用原始类型(例如
LocalizedAttribute
)@用户1692342
<T> List<LocalizedAttribute<T>> getLocalizedAttributes(List<LocalizedAttribute<B>> localizedAttributes, Function<B,T> mapper) {
    return localizedAttributes.stream()
        .map(localizedAttribute -> {
            Locale locale = localizedAttribute.getLocale();
            B b = localizedAttribute.getValue();
            return new LocalizedAttribute<T>(mapper.apply(b), locale);
        })
        .collect(Collectors.toList());
}
<T> List<LocalizedAttribute<T>> getLocalizedAttributes(List<LocalizedAttribute<B>> localizedAttributes, Function<B,T> mapper) {
    return localizedAttributes.stream()
        .map(la -> new LocalizedAttribute<T>(mapper.apply(la.getValue()), la.getLocale()))
        .collect(Collectors.toList());
}
List<LocalizedAttribute<String>> titleList = getLocalizedAttributes(list,B::getTitle);
List<LocalizedAttribute<String>> summaryList = getLocalizedAttributes(list,B::getSummary);