0

I have a Callable defined as :

public class ResponseRetriever implements Callable {

        @Override
        public ArrayList<Person> call() throws Exception {
            ArrayList<Person> list = new ArrayList<>();
            list.add(Person.builder().joinTime(System.currentTimeMillis()).build());
            return list;
        }
}

I am using future like this:

        int count = 5;
        ExecutorService executorService = Executors.newFixedThreadPool(count);
        List<Future<List<Person>>> result = new ArrayList<>();

        for (int i=0;i<count;i++) {
            result.add(executorService.submit(new ResponseRetriever()));
        }

        List<Person> personList = new ArrayList<>();
        for (Future f : result) {
            try {
                personList.addAll(f.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

I get following error in personList.addAll(f.get()); line :

java.util.List cannot be applied to java.lang.Object.

I understand the solution is to cast return value of f.get(). But I wanted to know the reason behind this error and if there is solution other than casting ?

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114

0 Answers0