Например, рассмотрим список → ["лев","лиса","заяц" ,"carrot"].
Вывод будет следующим:
Код: Выделить всё
lion eats fox
fox eats hare
hare eats carrot.
Код
Код: Выделить всё
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));
}
Код: Выделить всё
//consider the class name I am working with is StreamUtils.
StreamUtils.getAdjecentOverlappingStream(Arrays.asList("lion","fox","hare","carrot"))
.forEach(System.out::println);;
См. полная ошибка.
Ошибка
Код: Выделить всё
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
Подробнее здесь: [url]https://stackoverflow.com/questions/62834000/process-adjacent-overlapping-pairs-from-a-java-stream[/url]
Мобильная версия