Question from the Java and Craftsmanship test

This code allows to randomly get numbers between 1 to 31 in results. Should have declard SimpleDateFormat in the Thread.

Hard

This code

SimpleDateFormat format = new SimpleDateFormat("DD");

ExecutorService executor = Executors
    .newFixedThreadPool(10);

Set<Future<String>> days = Collections
    .newSetFromMap(new ConcurrentHashMap<>());

IntStream.range(1, 32)
    .forEach(day -> {
      Future<String> result = executor.submit(() -> {

        Date date = new GregorianCalendar(2021,
            Calendar.JANUARY, day).getTime();

        return format.format(date);
      });

      days.add(result);
    });

Set<String> results = days.stream()
    .map(return result -> {
         try {
           return result.get();
         } catch (InterruptedException | ExecutionException e) {
           throw new AssertionError(e);
         }
       })
    .collect(Collectors.toSet());
Author: Clément DevosStatus: PublishedQuestion passed 356 times
Edit
4
Community EvaluationsNo one has reviewed this question yet, be the first!