首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在HashMap中使用泛型?

如何在HashMap中使用泛型?
EN

Stack Overflow用户
提问于 2013-12-03 02:29:47
回答 2查看 166关注 0票数 1

我有两个方法,一个返回HashMap of <Integer,String>,另一个返回HashMap of <Integer, Spanned>。是否有一种方法可以使用泛型将它们变成一种方法?

代码语言:javascript
复制
public static Map<Integer, Spanned> queryGMGText() throws ParseException {
    ParseQuery<ParseObject> positionQuery = ParseQuery.getQuery("AndroidGMGContent");
    positionQuery.whereExists("position");
    List<ParseObject> ParsePositionResult = positionQuery.find();

    final Map<Integer,Spanned> appText = new HashMap<Integer,Spanned>();

    for (int i = 0; i < ParsePositionResult.size(); i++) {
        appText.put(ParsePositionResult.get(i).getInt("position"), Html.fromHtml(ParsePositionResult.get(i).getString("appText")));
    }

    return appText;
}

public static Map<Integer, String> queryGMG(String field) throws ParseException {
    ParseQuery<ParseObject> positionQuery = ParseQuery.getQuery("AndroidGMGContent");
    positionQuery.whereExists("position");
    List<ParseObject> ParsePositionResult = positionQuery.find();

    final Map<Integer,String> fieldMap = new HashMap<Integer,String>();

    for (int i = 0; i < ParsePositionResult.size(); i++) {
        fieldMap.put(ParsePositionResult.get(i).getInt("position"), ParsePositionResult.get(i).getString(field));
    }

    return fieldMap;
}

如果是这样,我将如何实例化并调用它们?目前,我是这样做的(从另一个班级):

代码语言:javascript
复制
Map<Integer,Spanned> appTextMap = new HashMap<Integer,Spanned>();
    try {
        appTextMap = ParseContent.queryGMGText();
    } catch (ParseException e) {
        e.printStackTrace();
    }
// now I can use it, i.e.
Spanned s = appTextMap.get(1);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-03 03:02:29

这使用单个映射,不需要显式转换,但确实需要知道每个键的类型。也许不是一个完美的契合,但也许这给你一些想法来建立正确的解决方案?希望能帮上忙

代码语言:javascript
复制
public class CustomMap {

      private Map<String, Object> _map;

      public CustomMap(){
         _map = new HashMap<String, Object>();  
      }  

      public <T> void setTypeValueByKey(Integer key, T value) {
         _map.put(key.toString(), value);
      }

      public <T> T getTypeValueByKey(Class<T> klass, Integer key) {
         return klass.cast(_map.get(key.toString()));
      }

      public static void main(String[] o){
         CustomMap map = new CustomMap();
         map.setTypeValueByKey(new Integer(1), new Spanned() );
         map.setTypeValueByKey(new Integer(2), new String("foo"));         

         Spanned spanned = 
           map.getTypeValueByKey(Spanned.class, new Integer(1));
         String foo = 
           map.getTypeValueByKey(String.class, new Integer(2));                  
      }

   static class Spanned{}
}

所以你有三个选择:

  • 使用Map<Integer,Object>、jkschneider解决方案的显式铸造
  • 以上解决方案的各个方面
  • 或者,Map<Integer,<? extends MyBaseClass>>,其中SpannedString-like类扩展了MyBaseClass
票数 0
EN

Stack Overflow用户

发布于 2013-12-03 02:41:50

严格回答问题:

由于SpannedString的继承树中没有共同性,所以您必须修改方法以返回Map<Integer, Object>,然后可以:

代码语言:javascript
复制
try {
    Map<Integer, Object> appTextMap = ParseContent.queryGMGText();
    // now I can use it, i.e.
    Spanned s = (Spanned) appTextMap.get(1);
} catch (ParseException e) {
    e.printStackTrace();
}

当然,这种做法违背了仿制药的目的,但却能在这里照亮真正的问题。

重新考虑原代码:

当然,将整件事情重写为如下内容似乎要好得多。这两个函数之间唯一的区别是Html.forHtml(..)调用。让我们假设一下(在不失去通用性的情况下),您真的想要String而不是Spanned

代码语言:javascript
复制
public static Map<Integer, String> queryGMGAppText() throws ParseException {
    return queryGMG("appText");
}

public static Map<Integer, String> queryGMG(String field) throws ParseException {
    ParseQuery<ParseObject> positionQuery = ParseQuery.getQuery("AndroidGMGContent");
    positionQuery.whereExists("position");
    List<ParseObject> ParsePositionResult = positionQuery.find();

    final Map<Integer,String> fieldMap = new HashMap<Integer,String>();

    for (int i = 0; i < ParsePositionResult.size(); i++) {
        fieldMap.put(ParsePositionResult.get(i).getInt("position"), ParsePositionResult.get(i).getString(field));
    }

    return fieldMap;
}

也许你根本不需要queryGMGAppText()。你在很多地方都这么称呼它,所以有这种方便的方法很重要吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20341742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档