Sometimes I see examples of using a diamond operator in the method invocation.
An example of syntax is:
.<ClassName>methodName()
I'm not sure that I understand entirely in which condition there is a case for such use. Didn't find an explanation of that in the references.
May you explain that case or suggest a source of information where I can find an explanation?
I am providing working an example down below. This example uses RxJava2.
There Flowable.<Integer>create(emitter -> emit(emitter), BackpressureStrategy.BUFFER) doesn't compile without <Integer> because the method emit accepts FlowableEmitter<Integer> emitter.
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.schedulers.Schedulers;
public class Sample {
public static void main(String[] args) {
Flowable.<Integer>create(emitter -> emit(emitter), BackpressureStrategy.BUFFER)
.observeOn(Schedulers.computation(), true,2)
.map(data -> data * 1)
.subscribe(Sample::process,
err -> System.out.println("ERROR: " + err),
() -> System.out.println("DONE"));
}
public static void process(int value) {
System.out.println(value);
sleep(1000);
}
private static void emit(FlowableEmitter<Integer> emitter) {
int count = 0;
while(count < 10) {
count++;
System.out.println("Emitting ..." + count);
emitter.onNext(count);
sleep(500);
}
}
private static boolean sleep(int ms) {
try {
Thread.sleep(ms);
return true;
} catch (InterruptedException e) {
return false;
}
}
}
When I try to simulate a similar case with plain Java it doesn't work. What I understand wrong?
public class Sample2 {
public static void main(String[] args) {
GenericClass.<Integer>genericMethod(param -> processParam(param));
}
private static void processParam(GenericClass<Integer> b) {
}
}
}
class GenericClass <T> {
public static <T> GenericClass<T> genericMethod(GenericInterface<T> genericinterface) {
return new GenericClass<T>();
}
}
interface GenericInterface <T> {
T doJob();
}