Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:39:01+00:00 2026-06-14T02:39:01+00:00

First I will try to explain the idea behind this code. I have a

  • 0

First I will try to explain the idea behind this code.
I have a bunch of classes (Processors) that can process a certain type of other classes (Processables). I have a List of Processors to execute them in a certain order. I have a Map that will retrieve me the data to process (Processables) for a certain Processor.
It looks somehow like this.

public abstract class AbstractProcessable {
    ...
}

public class DummyProcessable extends AbstractProcessable {
   ...
}

public abstract class AbstractProcessor<T extends AbstractProcessable> {
    public abstract void process(List<T> listOfProcessables);
}

public class DummyProcessor extends AbstractProcessor<DummyProcessable> {

    @Override
    public void process(List<DummyProcessable> listToProcess) {
        ...
    }
}

This seems to work fine so far. There are no compilation errors. But now I have a class like the following:

public class RandomClass {

    private List<AbstractProcessor<? extends AbstractProcessable>> processors;

    private Map<Class<? extends AbstractProcessor>, List<? extends AbstractProcessable>> data;

    public RandomClass() {
        processors = new ArrayList<>();
        processors.add(new DummyProcessor());

        data = new HashMap<>();
        data.put(DummyProcessor.class, new ArrayList<DummyProcessable>());

        processAll();
    }

    private void processAll() {
        for (AbstractProcessor<? extends AbstractProcessable> processor : processors) {
            List<? extends AbstractProcessable> dataToProcess;
            dataToProcess = data.get(processor);
            processor.process(dataToProcess); // compile error
        }
    }
}

Compile error:

    The method process(List<capture#4-of ? extends AbstractProcessable>) in the type AbstractProcessor<capture#4-of ? extends AbstractProcessable> is not applicable for the arguments (List<capture#5-of ? extends AbstractProcessable>)

I know it might be a bit difficult to read, but I tried to simplify it as much as possible. I’m also not that good with generics so maybe I used some wildcards wrong?
Can anyone help me to solve that problem?

Thanks a lot in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:39:02+00:00Added an answer on June 14, 2026 at 2:39 am

    If I understand your question correctly, I asked a similar one some time ago. Short answer is that you can’t use such a map in the way you intend to. The good news is that you don’t need it 😉

    The problem is that Java generics are a compile-time thing (as you may already know), and they solely exist at compile time, which is not when you actually fill your List. You cannot find a way to express your idea in Java, even if it seems a perfectly legitimate one.

    You may find Super Type Tokens useful to extract parameters in certain cases and avoid the user to supply an explicit parameter if it’s suitable (however mind the limitations and consider if they can really add some value).

    In short, you’ll end up with a field like

    private Map<Class<?>, List<?>> data;
    

    (or you can use Guava’s Multimap implementation for this), using some casts and shouting some warnings out, and relying on your program logic for the client code to be type safe. I used that approach in my code and it never failed so far.

    This is my code. I think it’s exactly your case, just replace Subscriber<T> and Message with your Processor<T> and Processable

    public class MessageBus {
    
        public enum Action {
            CREATE, REMOVE, UPDATE, DELETE;
        }
    
        private static Map<Class<?>, Set<Subscriber<?>>> subscriptions;
    
        static {
            subscriptions = new HashMap<Class<?>, Set<Subscriber<?>>>();
        }
    
        @SuppressWarnings("unchecked")
        public static <T> void publish(T message, Action action) {
            Set<Subscriber<?>> set = getSubscribersFor(message.getClass());
    
            if (set == null)
                return;
    
            for (Subscriber<?> subscriber: set) {
                ((Subscriber<T>) subscriber).onMessage(message, action);
            }
        }
    
        public static <T> void subscribe(Class<T> type, Subscriber<T> subscriber) {
            Set<Subscriber<?>> set = getSubscribersFor(type);
    
            if (set == null) {
                set = new HashSet<Subscriber<?>>();
                subscriptions.put(type, set);
            }
    
            set.add(subscriber);
        }
    
        public static <T> void unsuscribe(Class<T> type, Subscriber<T> subscriber) {
            Set<Subscriber<?>> set = getSubscribersFor(type);
            set.remove(subscriber);
        }
    
        private static Set<Subscriber<?>> getSubscribersFor(Class<?> topic) {
            return subscriptions.get(topic);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will try to explain this the best that I can. I have a
I will try to explain my problem as much as I can. I have
I will try and explain this without causing any confusion, first here is the
I will try to explain this the best way I can, but feel free
I will try to explain this very simple. So, I skipped any code which
Hi first of all apologies if this is poorly worded, will try to explain
I will try and explain this as concise as possible. I have 2 objects,
First I will try to explain what I want to do. The app loads
I will try to be specific if I can - please be patient, first
I will try and explain exactly what I want to achieve first. Imagine two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.