如何在单个集合中表示泛型类型?

public interface Rule<T> {
  String execute(T dataProvider);
}

public interface DataProvider {
   // No common functions as of now
}

public class RulesExecutor {
     private Map<String, List<Rule>> documentToRuleListMap;
    public RulesExecutor(Map<String, List<Rule>> documentToRuleListMap) {
        this.documentToRuleListMap = documentToRuleListMap;
    }

    public executeRules(DataProvider dataProvider, String document) {
      // some logic here
    }
}


是否可以存储RulesExecutor构造函数中所示的映射?

如果不是,存储此信息的正确数据结构是什么?

最佳答案

是否可以存储RulesExecutor构造函数中所示的地图?如果不是,什么是正确的数据结构来存储此信息?


当然:

public interface Rule<T> {
  String execute(T dataProvider)
}


public interface DataProvider {

}


class RulesExecutor<T extends DataProvider> {

   public RulesExecutor(Map<String, List<Rule<T>>> documentToRuleListMap) {
   }

   public void executeRules(T dataProvider) {
     // some logic here
   };
}




请注意,由于粗心的错误,您发布的代码将无法编译:

public interface Rule<T> {
  String execute(T dataProvider) // *** missing semicolon
}


public interface DataProvider {

}

public class RulesExecutor() { // *** class declarations have no parenthesis


发布代码时请多加注意,并确保发布真实代码,而不是同类代码。它使您更容易理解问题并在执行操作时获得帮助。

09-28 12:12