gpt4 book ai didi

java - 实现 map 功能

转载 作者:行者123 更新时间:2023-11-30 12:02:16 25 4
gpt4 key购买 nike

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

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 方法添加 < U >。

下面是我的完整代码:

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 类的构造函数之一存在编译时错误...

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 表达式。接口(interface) Function 中的抽象方法 apply 有一个参数,您的 lambda 表达式缺少该参数。所以构造函数代码应该是:

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

关于java - 实现 map 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58721937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com