我有一个类MyGen,它接受两个通用参数并有一个方法getValue()
public interface MyGen<E,T> {
T getValue();
}
Run Code Online (Sandbox Code Playgroud)
通常第二个泛型类型是Longor Integer。
然后,当第二个泛型类型Long如下时,我写了一个方法来组合对象:
public static <E extends MyGen<?, Long>> long combineValue(Set<E> set) {
return set.stream()
.map(MyGen::getValue)
.reduce(0L, (a,b) -> a | b);
}
Run Code Online (Sandbox Code Playgroud)
现在我想在第二种类型是Integer. 所以我尝试将上述相同的方法更新为:
public static <E extends MyGen<?, ? extends Number>> long combineValue(Set<E> set) {
return (long) set.stream()
.map(MyGen::getValue)
.reduce((a,b) -> a | b) // error1
.orElse(0); // error2
}
Run Code Online (Sandbox Code Playgroud)
但显示以下错误:
运营商 | 未定义参数类型 capture#4-of ?扩展 java.lang.Number, capture#4-of ? …
我有一个方法如下:
void updateObject(ObjOne obj, SomeClass data) {
if(obj != null) {
obj.doSomething(data);
}
}
Run Code Online (Sandbox Code Playgroud)
在updateObject多次调用,避免空检查在每一个地方,我想避免以下的:
// directly invoke doSomething conditionally.
if(obj != null) {
SomeClass data = getData();
obj.doSomething(data);
}
Run Code Online (Sandbox Code Playgroud)
由于data仅在obj非空时使用,我想到了如下重构代码:
void updateObject(ObjOne obj, Supplier<SomeClass> data) {
if(obj != null) {
obj.doSomething(data.get());
}
}
Run Code Online (Sandbox Code Playgroud)
这将SomeClass仅在需要时创建一个对象,而是创建一个Supplier类型的对象。
上述方法使用Supplier更好吗?
我有一个计算平均成本的流,代码看起来像这样
private Double calculateAverageCost(List<Item> items) {
return items.stream()
.mapToDouble(item -> item.cost)
.filter(cost -> cost > 0) // Ignore zero cost items
.average()
. // Something here to convert an OptionalDouble to value or null
}
Run Code Online (Sandbox Code Playgroud)
我需要返回值或null没有值的方法(例如,当所有成本为零时)。我的问题是不存在orElseNull的方法OptionalDouble做转换。我可以在另一个步骤中完成,例如
private Double calculateAverageCost(List<Item> items) {
final OptionalDouble average = items.stream()
.mapToDouble(item -> item.cost)
.filter(cost -> cost > 0) // Ignore zero cost items
.average();
return average.isPresent() ? average.getAsDouble() : null;
}
Run Code Online (Sandbox Code Playgroud)
我意识到这是一个“原始”流,我的方法返回盒装,Double但看起来这可能会有所帮助,类似于 ( Optional.empty().orElseGet(null))。
我缺少原因或更好的解决方案吗?
我正在浏览 java 8 中引入的 Predicate 类,它是函数式接口。Predicate 类中有一个方法和方法,用于将多个谓词组合成一个。
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了 Java 中 PECS 的概念,但仍然无法理解为什么在 Predicate 的情况下我们使用? super T. Java 程序员如何决定它将成为消费者而不是生产者。
我的意思是为什么不允许出现编译错误的行:
public class PredicateExample {
public static void main(String[] args) {
Predicate<Number> pred = n -> n.intValue() > 2;
Predicate<Integer> predInt = n -> n > 3;
//Compile error
//pred.and(predInt);
Predicate<Object> predObj = o -> Integer.parseInt(o.toString()) > 4;
pred.and(predObj); //Valid statement
Number n = new …Run Code Online (Sandbox Code Playgroud) IntStream.range(x,y)将从x(inclusive)和y(exclusive)返回一个流。
IntStream.rangeClosed(x,y)将从x(inclusive)和y(inclusive)返回一个流。
我希望rangeClosed(x,y)调用range(x,y-1)或range(x,y)调用rangeClosed(x,y-1). 但是在查看range它的源代码时,它就像:
if (startInclusive >= endExclusive) {
return empty();
} else {
return StreamSupport.intStream(
new Streams.RangeIntSpliterator(startInclusive, endExclusive, false /*not closed*/), false);
}
Run Code Online (Sandbox Code Playgroud)
该rangeClosed也有一个非常相似的实现,而不是range(x,y+1)。唯一的区别是,第三个参数Streams.RangeIntSpliterator是true代替false表示该范围被关闭。
然后使用此布尔值来初始化类中的int last字段,Streams.RangeIntSpliterator并针对它提到了以下注释:
1 如果范围是封闭的并且最后一个元素还没有被遍历 否则 0 如果范围是开放的,或者是一个封闭的范围并且所有元素都已经被遍历
为什么这样的实现是必要的,而不是range简单地调用rangeClosed或相反?调用rangeClosed(x,y)而不是调用之间有什么显着区别range(x,y+1)吗?
不String.isBlank()等于String.isEmpty() + *whitespace only check*?在查看 的源代码时String.isBlank(),它会在长度为零时返回 true。但我想知道是否有任何场景isEmpty()会为相同的输入返回 atrue并isBlank()返回 a false。
题
我无法理解“可重用面向对象软件的元素”中观察者模式的以下 UML 图。谁能解释我为什么我的 Java 实现是错误的,我必须改变什么才能正确实现它?
UML-图
尝试(但错误)实施
public interface Subject {
public static final List<Observer> observers = new ArrayList<Observer>();
public void attach(Observer o);
public void detach(Observer o);
public void notifyObservers();
}
public interface Observer {
public void update();
}
public class ConcreteSubject implements Subject {
private String subjectState;
@Override
public void attach(Observer o) {
observers.add(o);
}
@Override
public void detach(Observer o) {
observers.remove(o);
}
@Override
public void notifyObservers() {
for (Observer o : observers) {
o.update();
} …Run Code Online (Sandbox Code Playgroud) 您好,我需要在我的 Java 数组中用 0 替换 Nan 值,任何帮助,谢谢。你可以找到我的数组的例子。
数组类型为double,数组内容如下。
x = [ 1 , 2, Nan , 4, Nan ]
Run Code Online (Sandbox Code Playgroud) 假设Streams和Collections,Lambdas可以使用吗?我尝试使用 for 循环,但它没有解决我的问题。
// Set<Set<String>> to Set<String>
for(Set<String> s : set) {
result.addAll(s);
set.add(result);
}
Run Code Online (Sandbox Code Playgroud)
set 是一种Set<Set<String>>类型, result 是Set<String>.