简体   繁体   English

map()方法的通配符泛型

[英]Wildcard generics of map() method

This is the implementation of map() method: 这是map()方法的实现:

public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
    Objects.requireNonNull(mapper);
    if (!isPresent()) {
        return empty();
    } else {
        return Optional.ofNullable(mapper.apply(value));
    }
}

When I call map() like this, what is the type of T and U ? 当我像这样调用map()TU的类型是什么? What is the type of wildcard ( ? )? 通配符的类型是什么( ? )? It is very confusing. 这非常令人困惑。

Optional<String> os1 = Optional.of("Optional String");
Optional<String> os2 = os1.map(s -> s.toUpperCase());

Javadoc statement says: Javadoc声明说:

@param <U> is the type of the value returned from the mapping function. @param <U>是映射函数返回的值的类型。

Does " mapping function " mean map() method or argument of the map() ? 是否“ 映射函数 ”是指map()的方法或参数map()

Regardless if it's Stream or Optional , the purpose of the method map which accepts Function<? super T, ? extends U> 无论是Stream还是Optional ,接受Function<? super T, ? extends U>的方法map的目的是Function<? super T, ? extends U> Function<? super T, ? extends U> Function<? super T, ? extends U> is to map each input value T to an output of the same or different type U . Function<? super T, ? extends U>是将每个输入值T映射到相同或不同类型U的输出。 The wildcard is used for extending the range of mapping possibilities. 通配符用于扩展映射可能性的范围。

Does " mapping function " mean map() method or argument of the map() ? 是否“ 映射函数 ”是指map()的方法或参数map()

The " mapping function " is a T -> U mapping implementation of an anonymous class of the Function interface which might be shortened with a lambda expression or a method reference. 映射函数 ”是Function接口的匿名类的T - > U映射实现,可以使用lambda表达式或方法引用缩短它。

In your sample, the T and U are both String since you map the String to its upper-case variation which is a String again. 在您的示例中, TU都是String因为您将String映射到其大写变体,这又是一个String It behaves the same like UnaryOperator<T> a special case of Function<T, T> which consumes and returns the same type. 它的行为与UnaryOperator<T>相同,是Function<T, T>一种特殊情况,它使用并返回相同的类型。

On the other hand, if you map: 另一方面,如果您映射:

os1.map(s -> s.length())
  • The T is String TString
  • The U is Integer since the method String::length produces an integer UInteger因为方法String::length产生一个整数

You might shorten the lambda with a method reference: 您可以使用方法引用缩短lambda:

Optional<String> os2 = os1.map(String::toUpperCase);

... or better use both of the Optional s together: ...或者更好地同时使用两个可Optional

Optional<String> os1 = Optional.of("Optional String");
                               .map(String::toUpperCase);

Firstly, just to answer your question of: 首先,只是回答你的问题:

Does "mapping function" mean map() method or argument of the map()? “映射函数”是指map()的map()方法或参数吗?

This "mapping function" refers to the argument supplied to the map method of the Optional<T> type. 此“映射函数”是指提供给Optional<T>类型的map方法的参数。

To make things easier to understand, just forget about the wildcard ( ? ) for a moment as we could do without its confusion and focus on the type T and U , the simplified version below: 为了使事情更容易理解,只需忘记通配符( ? ),我们就可以在没有混淆的情况下做到这一点,并专注于类型TU ,简化版本如下:

public <U> Optional<U> map(Function<T, U> mapper) { ... }
  • Where T represents any type of object as input and U represents any type as output. 其中T表示任何类型的对象作为输入, U表示任何类型作为输出。
  • Function<T, U> mapper represents a function taking an object of type T and returns a value of type U Function<T, U> mapper表示采用类型T的对象并返回类型U的值的函数
  • Optional<U> is an Optional encapsulating an object of type U . Optional<U>是一个可选的封装U类型的对象。

You could visualize the mapper function as: 您可以将映射器函数可视化为:

         _ _ _ _ _ _ _ _ _ 
        |                 |
        |                 |
T ----->|      logic      | -----> U
        |                 |
        |_ _ _ _ _ _ _ _ _|

So, given the above visualisation and your example of: 因此,鉴于以上可视化和您的示例:

Optional<String> os1 = Optional.of("Optional String");
Optional<String> os2 = os1.map(s -> s.toUpperCase());

The function passed to the map method of the Optional type ( s -> s.toUpperCase() ) is Function<String, String> mapper ie T in the visualization above becomes String and U in the visualization above becomes String ie 传递给Optional类型的map方法的函数( s -> s.toUpperCase() )是Function<String, String> mapper即上面的可视化中的T变为String ,上面的可视化中的U变为String

             _ _ _ _ _ _ _ _ _ 
            |                 |
            |                 |
  String -->|      logic      | ---> String
            |                 |
            |_ _ _ _ _ _ _ _ _|

So, when the above function is invoked by the map method of the Optional type, it will perform some operation represented by the "logic" above in the visualization, in this specific case the logic is simply converting the input string to an uppercase string. 因此,当可选类型的map方法调用上述函数时,它将在可视化中执行上面“逻辑”表示的某些操作,在这种特定情况下,逻辑只是将输入字符串转换为大写字符串。

Once that is done, the map method of the Optional<T> type wraps that uppercase string into an Optional object hence it returns an Optional<String> . 完成后, Optional<T>类型的map方法将该大写字符串包装到Optional对象中,因此它返回Optional<String>

Seems to me you have a wrong src (with Javadoc) project of you JDK. 在我看来,你有一个错误的src(使用Javadoc)项目JDK。 Consider what the Oracle Docs about Optional in Java 8 says: 考虑Java 8中关于OptionalOracle Docs所说的内容:

map(Function mapper) map(函数映射器)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. 如果存在值,则将提供的映射函数应用于该值,如果结果为非null,则返回描述结果的Optional。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM