I want to create a method that prints out information within a stream. Please take a look at my existing logger method. The method arguments have to be generic like in my example string & integer. The method is supposed to return the original object, in my case string. Can someone tell me what's wrong with my current method?
Thanks a lot in advance!
look at the logger-method
Stream<String> stream = Stream.of("A", "BC", "XYZ");
stream.map(t -> logger(t.length()))
.map(t-> logger(t.substring(0, 2)))
.collection(Collectors.toList());
public static <T> T logger(T t) {
System.out.println(t);
return t;
}
ERROR: Cannot infer type argument(s) for map(Function)
This looks like an example of where you might want to use .peek()
instead of a map. It's specifically built for printing out, and you don't need to worry about returning anything!
It might look more like:
stream.peek(it -> System.out.println(it.length))
.peek(it -> System.out.println(it.substring(0, 2))
// presumably other functions here before collecting...
.collect(Collectors.toList());
Take a look at the official documentation here: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-
To address your previous question, you probably want to update your function so that it takes a pure example of T, and a transform for T. You can update it to look like:
public static <T, R> T logger(T t, Function<T, R> transform) {
R logLine = transform.apply(t);
System.out.println(logLine);
return t;
}
and your code will look like
stream.map(element -> logger(element, element::length)) // and so on
The problem I face here is that the first map is return an integer, so you are trying to use Integer.substring
which does not exists.
What I recommend in your case is to use peek
instead of map
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class InferType {
public static void main(String[] args) {
Stream<String> stream = Stream.of("A", "BC", "XYZ");
stream.peek(t -> logger(t.length()))
.peek(t-> logger(t.substring(0, 2)))
.collect(Collectors.toList());
}
public static <T> void logger(T t) {
System.out.println(t);
}
}
This works on my end
import java.util.stream.*;
class Main {
public static void main(String[] args) {
Stream<String> stream = Stream.of("AX", "BC", "XYZ");
stream.map(t -> logger(t))
.map(t-> logger(t.substring(0, 1)))
.collect(Collectors.toList());
/* or this
stream.map(t -> logger(t))
.collect(Collectors.toList());
*/
}
public static <T> T logger(T t) {
System.out.println(t);
return t;
}
}
Have a look at what a stream() map() really does here . But basically, it says
Stream map(Function mapper) returns a stream consisting of the results of applying the > given function to the elements of this stream.
edit: @Ezequiel made a good point about Integer.substring
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.