Witam uczę się podstaw dotyczących wyrażeń lambda w Javie. Zefiniowałem następującą funkcję:
public static void repeat(int n, Runnable action) {
for(int i = 0; i < 0; i++) {
action.run();
}
}
I ją wywołałem
repeat(10, run -> System.out.println("Witaj świecie!"));
Jest to kod przepisany z książki i nie działa, pojawiają się następujące komunikat
Multiple markers at this line
- The method repeat(int, Runnable) in the type Lambdy is not applicable for the arguments (int, (<no type>
run) -> {})
- Lambda expression's signature does not match the signature of the functional interface method run()
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method repeat(int, Runnable) in the type Lambdy is not applicable for the arguments (int, (<no type> run) -> {})
Lambda expression's signature does not match the signature of the functional interface method run()
at com.gmail.aspoka1.Lambdy.main(Lambdy.java:47)
Odziwo po dodaniu następnej finkcji
public static void repeat(int n, IntConsumer action) {
for(int i = 0; i < n; i++) {
action.accept(i);
}
}
Program działa prawodłowo i jako wynik w konsoli wypisuje się 10x Witaj świecie!
Czy ktoś może mi wyjaśnić dlaczego tak się dzieje i jak wywołać pierwszą funkcje by od początku działało prawidłowo?