invokeAll Callable tasks

Report a typo

Let's create the ExecutorService with a single thread and execute a set of Callable tasks with String objects in it. You have to run them all with the invokeAll method. Output the longest String object you get. If there are several words of equal length, choose the one that comes first in alphabetical order. Don't forget to stop the service.

Sample input 1:

4
Java
Computer
Data
COBOL

Sample output 1:

Computer

Sample input 2:

3
euphoria
assembly
go

Sample output 2:

assembly

Sample Input 1:

4
Java
Computer
Data
COBOL

Sample Output 1:

Computer
Write a program in Java 17
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.*;
import java.util.stream.IntStream;

class InvokeAll {
public static void main(String[] args) {
List<Callable<String>> tasks = getTasks(); //execute these tasks
// write your code here
}

// Don't change the code below
private static List<Callable<String>> getTasks() {
Scanner scanner = new Scanner(System.in);
int count = scanner.nextInt();
scanner.nextLine(); // read rest of the first line
return IntStream
.rangeClosed(1, count)
.mapToObj(i -> (Callable<String>) scanner::next)
.toList();
}
}
___

Create a free account to access the full topic