我有两个方法,一个返回HashMap of <Integer,String>,另一个返回HashMap of <Integer, Spanned>。是否有一种方法可以使用泛型将它们变成一种方法?
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;
}如果是这样,我将如何实例化并调用它们?目前,我是这样做的(从另一个班级):
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);发布于 2013-12-03 03:02:29
这使用单个映射,不需要显式转换,但确实需要知道每个键的类型。也许不是一个完美的契合,但也许这给你一些想法来建立正确的解决方案?希望能帮上忙
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>>,其中Spanned和String-like类扩展了MyBaseClass发布于 2013-12-03 02:41:50
严格回答问题:
由于Spanned和String的继承树中没有共同性,所以您必须修改方法以返回Map<Integer, Object>,然后可以:
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
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()。你在很多地方都这么称呼它,所以有这种方便的方法很重要吗?
https://stackoverflow.com/questions/20341742
复制相似问题