by Guest » 03 Jan 2025, 08:28
Ich arbeite an einem Problem, bei dem ich überlappende Paare aus dem Stream verarbeiten muss.
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.
Die Ausgabeelemente sind immer um eins kleiner als die der ursprünglichen Liste. Ich verwende derzeit Java 8. Hier ist mein Code:
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));
}
Aufrufende Methode
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);;
Aber dieser Code gibt mir den Fehler
Fehler: Typargumente für Funktion können nicht abgeleitet werden
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
Ich arbeite an einem Problem, bei dem ich überlappende Paare aus dem Stream verarbeiten muss.
Betrachten Sie zum Beispiel die Liste → ["Löwe", "Fuchs", "Hase" ,"carrot"].
Die Ausgabe wird sein:
[code]lion eats fox
fox eats hare
hare eats carrot.
[/code]
Die Ausgabeelemente sind immer um eins kleiner als die der ursprünglichen Liste. Ich verwende derzeit Java 8. Hier ist mein Code:
Code
[code]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]
Aufrufende Methode
[code]//consider the class name I am working with is StreamUtils.
StreamUtils.getAdjecentOverlappingStream(Arrays.asList("lion","fox","hare","carrot"))
.forEach(System.out::println);;
[/code]
Aber dieser Code gibt mir den Fehler [b]Fehler: Typargumente für Funktion können nicht abgeleitet werden[/b]
Siehe Vollständiger Fehler.
Fehler
[code]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