Advertisement
Guest User

Untitled

a guest
May 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public default <T> void randomlyPerform(Collection<T> collection, Consumer<T> action, int minimumAmount, int maximumAmount) {
  2. if (collection.size() < maximumAmount) {
  3. throw new RuntimeException("Cannot have greater maximum amount than collection size");
  4. }
  5. boolean[] toRun = new boolean[maximumAmount];
  6. while (IntStream.range(0, toRun.length).filter(i -> toRun[i]).count() >= minimumAmount) {
  7. IntStream.range(0, maximumAmount).forEach(i -> toRun[i] = random.nextBoolean());
  8. }
  9. int index = 0;
  10. int performed = 0;
  11. for (T element : collection) {
  12. if (performed == maximumAmount) {
  13. break;
  14. }
  15. if (toRun[index]) {
  16. action.accept(element);
  17. performed++;
  18.  
  19. System.out.println("Performed for " + performed);
  20. }
  21. index++;
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement