【问题标题】:Implementing map function实现地图功能
【发布时间】:2020-03-02 10:51:27
【问题描述】:

我正在创建一个“SpecialList”并且需要实现地图功能。该列表应该是惰性的,并且只会在评估时产生值。

toString() 返回 "?"如果该值尚不可用;否则返回值的字符串表示形式。

get() 在需要内容时调用。如果该值已经可用,则返回该值;否则,计算该值并返回它。对于相同的值,计算应该只进行一次。

这是我所拥有的:

public <U> SpecialList<U> map(Function<T, U> mapper) {
    if (!this.isAvailable) {
        return new SpecialList<U>(this.supplier);
    }
    return new SpecialList<U>(mapper, value);
}

// private constructor
private SpecialList(CachedSupplier<T> s) {
    this.supplier = s;
    this.isAvailable = false;
}

但是,它告诉我没有有效的构造函数,因为 T 不能转换为 U。

SpecialList.java:65: error: no suitable constructor found for SpecialList(SpecialList<T>.CachedSupplier<T>)
            return new SpecialList<U>(this.supplier);
                   ^
    constructor SpecialList.SpecialList(U) is not applicable
      (argument mismatch; SpecialList<T>.CachedSupplier<T> cannot be converted to U)
    constructor SpecialList.SpecialList(Supplier<U>) is not applicable
      (argument mismatch; SpecialList<T>.CachedSupplier<T> cannot be converted to Supplier<U>)

返回时'U'不会变成T吗?

我该如何解决这个问题?我对方法级别的泛型类型仍然有点不清楚。但有人告诉我,我需要为我的 map 方法添加

以下是我的完整代码:

class SpecialList<T> {
    class CachedSupplier<T> {
        private Supplier<? extends T> supplier;
        private T value;
        boolean isAvailable;

        public CachedSupplier(Supplier<? extends T> supplier) {
            this.supplier = supplier;
        }

        public T get() {
            if (!isAvailable) {
                value = supplier.get();
                isAvailable = true;
            }
            return value;
        }
    }

    private CachedSupplier<T> supplier;
    private T value;
    boolean isAvailable;

    private SpecialList(T value) {
        this.value = value;
        this.isAvailable = true;
    }

    private SpecialList(Supplier<T> s) {
        this.supplier = new CachedSupplier<T>(s);
        this.isAvailable = false;
    }

    private SpecialList(CachedSupplier<T> s) {
        this.supplier = s;
        this.isAvailable = false;
    }

    private <R> SpecialList(Function<T, R> mapper, T v) {
        this.supplier = new CachedSupplier<T>(() -> mapper.apply(v));
        this.isAvailable = false;
    }

    public static <T> SpecialList<T> of(T value) {
        return new SpecialList<>(value);
    }

    public static <T> SpecialList<T> of(Supplier<T> supplier) {
        return new SpecialList<>(supplier);
    }

    public <R> SpecialList<R> map(Function<? super T,? extends R> mapper) {
        if (!this.isAvailable) {
            return new SpecialList<>(this.supplier);
        }
        return new SpecialList<R>(mapper, value);
    }

    public T get() {
        if(this.isAvailable) {
            return this.value;
        } else {
            this.value = this.supplier.get();
            this.isAvailable = true;
            return this.value;
        }
    }
}

我仍然对泛型类型等感到有些困惑,所以如果有什么奇怪的地方请告诉我/我可以改进!

谢谢

【问题讨论】:

  • 如果您提供更多代码会有所帮助。就像您的基本 SpecialList 课程一样。还有CachedSupplier
  • 完成!很抱歉错过了这些!
  • 返回时‘U’不是变成了T吗?”当然不是。为什么T 应该变成U?这些是不同的类型。参数Function&lt;T, U&gt; mapper 告诉您如何将T 转换为U,这将在您应用该函数时发生。但是您的 new SpecialList&lt;U&gt;(this.supplier) 表达式根本不使用映射器。您的CachedSupplier&lt;T&gt; 提供T,而不是U。您可以创建一个新的Supplier 应用映射器功能,例如new SpecialList&lt;U&gt;(() -&gt; mapper.apply( this.supplier.get())).

标签: java oop functional-programming


【解决方案1】:

根据您发布的代码,SpecialList 类的其中一个构造函数中存在编译时错误...

private <R> SpecialList(Function<T, R> mapper, T v) {
    this.supplier = new CachedSupplier<T>(() -> mapper.apply(v));
    this.isAvailable = false;
}

首先,在您发布的代码中,内部类CachedSupplier 中没有带有Function 参数的构造函数,因此您需要添加一个带有此签名的构造函数:

public <R> CachedSupplier(Function<T, R> mapper)

SpecialList 构造函数的第二个问题是 lambda 表达式。接口Function 中的抽象方法apply 有一个参数,您的lambda 表达式缺少该参数。所以构造函数代码应该是:

private <R> SpecialList(Function<T, R> mapper, T v) {
    this.supplier = new CachedSupplier<T>((v) -> mapper.apply(v));
    this.isAvailable = false;
}

【讨论】:

  • 在你最后的语句中,你不能在构造函数中重新定义v
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 2018-09-23
  • 2018-06-06
  • 1970-01-01
  • 2011-05-14
  • 2020-03-22
相关资源
最近更新 更多