通用类型的集合

时间:2013-08-11 02:41:19

标签: java generics collections

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

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 sconstructor?

中所示存储地图

如果没有,将此信息存储在哪个正确的数据结构中?

1 个答案:

答案 0 :(得分:1)

  

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

不确定

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

发布代码时请务必小心,并确保发布真正的代码,而不是 sort-of 种类代码。它使你更容易理解你的问题并在你做的时候提供帮助。