java 将 Optional<Integer> 转换为 Optional<Long> 的好方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30729925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:34:19  来源:igfitidea点击:

Good way to convert Optional<Integer> to Optional<Long>

javajava-7guavaoptional

提问by drorsun

I am trying to find a clean and code-efficient way to convert Optional<Integer>to Optional<Long>. I am working in Java 7 with Guava.

我试图找到一种干净且代码高效的方式来转换Optional<Integer>Optional<Long>. 我正在使用 Guava 使用 Java 7。

So in one place in the code I have an optional integer created

所以在代码的一个地方我创建了一个可选的整数

    Optional<Integer> optionalInt = Optional.fromNullable(someInt);

And in another area I need it as an optional long. The nicest thing I could come up with is this:

在另一个领域,我需要它作为一个可选的长。我能想到的最好的事情是:

    Optional<Long> optionalLong = optionalInt.transform(new Function<Integer, Long>() {
        @Override
        public Long apply(Integer inputInt) {
            if (inputInt != null)
                return inputInt.longValue();
            else
                return null;
        }
    });

But this is cumbersome, especially if you consider how easy it was to cast the type when I was using primitive types.

但这很麻烦,特别是如果您考虑到在我使用原始类型时转换类型是多么容易。

Any good ideas out there?

有什么好的想法吗?

采纳答案by Boris the Spider

TL;DR: In Java 7, No.

TL;DR:在 Java 7 中,没有。

Sadly this is the best Java 7 has to offer in terms of support for functions.

遗憾的是,就函数支持而言,这是 Java 7 所能提供的最好的。

I would just say that transformwill never be called with nullso you can do:

我只想说transform永远不会被调用,null所以你可以这样做:

Optional<Long> optionalLong = optionalInt.transform(new Function<Integer, Long>() {
    @Override
    public Long apply(Integer inputInt) {
        return inputInt.longValue();
    }
});

From the documentation:

文档

If the instance is present, it is transformed with the given Function; otherwise, absent()is returned. If the function returns null, a NullPointerExceptionis thrown.

如果实例存在,则使用给定的 进行转换 Function;否则,absent()返回。如果函数返回 nullNullPointerException则抛出 a。

So neverreturn nullfrom a Functionpassed to transform.

所以从不返回nullFunction传递transform

If you reuse this a lot, then you could use the enumsingleton pattern:

如果你经常重用它,那么你可以使用enum单例模式:

public enum IntToLong implements Function<Integer, Long> {

    INSTANCE;

    @Override
    public Long apply(Integer input) {
        return input.longValue();
    }
}

Then:

然后:

optionalInt.transform(IntToLong.INSTANCE);

This obviously reduces the code at the call site at the expense of having extra classes in the code base - something I wouldn't be too worried about.

这显然减少了调用站点的代码,代价是代码库中有额外的类——我不会太担心这一点。

回答by Remigius Stalder

close to the cast:

接近演员表:

Optional<Long> optionalLong = Optional.fromNullable(optionalInt.isPresent() ?
     optionalInt.get().longValue() : null);

basically this avoids the overhead of invoking transform. Invoking isPresent could be simplified to checking the value for null directly.

基本上这避免了调用转换的开销。调用 isPresent 可以简化为直接检查 null 的值。