Created
September 19, 2020 09:59
-
-
Save SiAust/c9ff4181eb393fbc7cd40478035b1fad to your computer and use it in GitHub Desktop.
A functional supplier that returns incremented int
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.*; | |
class FunctionUtils { | |
public static Supplier<Integer> getInfiniteRange() { | |
int[] n = {0}; | |
return new Supplier<Integer>() { | |
@Override | |
public Integer get() { | |
return n[0]++; | |
} | |
}; | |
} | |
public static void main(String[] args) { | |
Supplier<Integer> sup = getInfiniteRange(); | |
for(int i = 0; i < 5; i++) { | |
System.out.print(sup.get() + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment