void () { List<Integer> numberOne = Collections.singletonList(1); List<Integer> collectionBeingModifiedSometimes = new ArrayList<>(); List<Integer> numberTwo = numberOne.stream().flatMap(entry -> { if (Math.random() > 0.5) { collectionBeingModifiedSometimes.add(999); } return Stream.of(entry * 2); }).collect(Collectors.toList()); String text = collectionBeingModifiedSometimes.size() == 0 ? "empty" : "nonempty"; System.out.println("" + numberOne + " " + numberTwo + " " + text); }