How to add List<Future<Object>> into Set<Object>?

VIRICH :

during the development of ExecutorService, it became necessary to put List in Set . How can this be done?

public class Executor {
    private Set<List<Future<Object>>> primeNumList = Collections.synchronizedSet(new TreeSet<>());

    Set<List<Future<Object>>> getPrimeNumList() {
        return primeNumList;
    }

    @SuppressWarnings("unchecked")
    public void setup(int min, int max, int threadNum) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
        List<Callable<Object>> callableList = new ArrayList<>();

        for (int i = 0; i < threadNum; i++) {
            callableList.add(new AdderImmediately(min + i, max, threadNum));
        }
        List<Future<Object>> a = executorService.invokeAll(callableList);
        primeNumList.add(a); // here i try to add Future list into Set
        System.out.println(primeNumList);
        executorService.shutdown();
    }

My class in which I process the values ​​and return them via call (). After that they fall into the List from where I want them to be placed in the final Set

public class AdderImmediately implements Callable {
    private int minRange;
    private int maxRange;
    private Set<Integer> primeNumberList = new TreeSet<>();
    private int step;

    AdderImmediately(int minRange, int maxRange, int step) {
        this.minRange = minRange;
        this.maxRange = maxRange;
        this.step = step;
    }

    @Override
    public Object call() {
        fillPrimeNumberList(primeNumberList);
        return primeNumberList;
    }

    private void fillPrimeNumberList(Set<Integer> primeNumberList) {
        for (int i = minRange; i <= maxRange; i += step) {
            if (PrimeChecker.isPrimeNumber(i)) {
               primeNumberList.add(i);
            }
        }
    }
}

Is it somehow possible to implement? Because what I have now, I get a ClassCastException. Or am I not understanding something?)

Exception:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Comparable
    at java.util.TreeMap.compare(TreeMap.java:1294)
    at java.util.TreeMap.put(TreeMap.java:538)
    at java.util.TreeSet.add(TreeSet.java:255)
    at java.util.Collections$SynchronizedCollection.add(Collections.java:2035)
    at Executor.setup(Executor.java:22)
    at Demo.main(Demo.java:47)
curlyBraces :

You are not able to catch the error at compile time because you have used @SuppressWarnings("unchecked"). On removing that, there's a compile warning at this statement: callableList.add(new AdderImmediately(min + i, max, threadNum));

The second problem is, you haven't used generic form while creating AdderImmediately class. You are clearly returning, Set<Integer> type from the call method. If you use the proper generic form in your case, i.e., Callable<Set<Integer>>, the problem becomes clear in the above line. The type of callableList is List<Callable<Object>>. You cannot add an element of type Callable<Set<Integer>> into it.

Because you had added the elements of incorrect type by suppressing generic warnings, you are getting ClassCastException at runtime.

I'd recommend you to read the chapters on Generics from Effective Java 3rd edition to better understand these concepts.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=87350&siteId=1