Betrachten Sie zum Beispiel die Liste → ["Löwe", "Fuchs", "Hase" ,"carrot"].
Die Ausgabe wird sein:
Code: Select all
lion eats fox
fox eats hare
hare eats carrot.
Code
Code: Select all
static Function pairMap(BiFunction mapper)
{
return new Function () {
T previous = null;
boolean hasPrevious;
public Stream apply(T t)
{
Stream result;
if(!hasPrevious)
{
hasPrevious = true;
result = Stream.empty();
}
else
{
result = Stream.of(mapper.apply(previous, t));
}
previous = t;
return result;
}
};
}
static Stream getAdjecentOverlappingStream(List list)
{
return list.stream().flatMap(pairMap((a,b) -> a+" eats "+ b));
}
Code: Select all
//consider the class name I am working with is StreamUtils.
StreamUtils.getAdjecentOverlappingStream(Arrays.asList("lion","fox","hare","carrot"))
.forEach(System.out::println);;
Siehe Vollständiger Fehler.
Fehler
Code: Select all
StreamLecture.java:83: error: cannot infer type arguments for Function
return new Function () {
^
reason: cannot use '' with anonymous inner classes
where T,R are type-variables:
T extends Object declared in interface Function
R extends Object declared in interface Function
StreamLecture.java:106: error: incompatible types: inference variable R#1 has incompatible bounds
return list.stream().flatMap(pairMap((a,b) -> a+" eats "+ b));
^
equality constraints: T#2
lower bounds: String,R#2
where R#1,T#1,T#2,R#2,T#3 are type-variables:
R#1 extends Object declared in method flatMap(Function