How to say“A Class of Class<?>列出< The class>"在Java中? [英] How to say "A Map of Class<?> to List<The class>" in Java?
问题描述
HashMap<?,List<>> map = new HashMap<>();
map.put(String.class,new ArrayList< Long>());
以下代码将被编译。
但是,我想编译失败,因为ArrayList不适用于String类型。
另外,我的通配符仅限于某个特定的接口(例如Exception),所以我想我应该把< ;?扩展异常>
某处。
如何实现上述功能?测试:
map.put(String.class,new ArrayList< String>()); //因为String不是一个异常而失败
map.put(IOException.class,new ArrayList< FileNotFoundException>()); // File失败,因为FileNotFoundException不是IOException,即使它是它的子类
map.put(FileNotFoundException.class,new ArrayList< IOException>()); //我想我对此很好。
map.put(IllegalArgumentException.class,new ArrayList< IllegalArgumentException>()); //成功
map.put(NumberFormatException.class,new ArrayList< ServerException>()); //再次失败,因为两个类不匹配
map.put(ClientDecodingException.class,new ArrayList< ClientDecodingException.class>); //因为映射采用通配符,所以再次成功例外
我已经通过使用raw Class
和 List
(仍然不能修复此问题) ),并通过使用映射包装来存储 Exception
:
public class ExceptionMapWrapper {
private Map< Class,List> MYMAP;
public ExceptionMapWrapper(){
myMap = new HashMap<>();
}
//测试的相关方法:put和get
public< T extends Exception> void put(Class< T> clazz,List< T> list){
myMap.put(clazz,list);
}
public< T extends Exception>列表与LT; T> get(Class< T> key){
返回myMap.get(key);
$ / code $ / pre
以及一个简单的测试:
ExceptionMapWrapper exceptionMapWrapper = new ExceptionMapWrapper();
Class< IOException> clazz = IOException.class;
列表< IOException> list = new ArrayList<>();
exceptionMapWrapper.put(clazz,list);
//编译器错误,取消注释以查看它们
//exceptionMapWrapper.put(String.class,new ArrayList< String>());
//exceptionMapWrapper.put(IOException.class,new ArrayList< ClassCastException>());
//exceptionMapWrapper.put(IOException.class,new ArrayList< SQLException>());
列表< IOException> ioExList = exceptionMapWrapper.get(clazz);
//编译器错误,取消注释
//列表< SQLException> sqlExList = exceptionMapWrapper.get(clazz);
Say I have a
HashMap<?, List<?>> map = new HashMap<>();
map.put(String.class, new ArrayList<Long>());
The following code will compile.
However, I want to fail compilation because the ArrayList is not for String type.
Also, my wildcard is limited to some specific interface (e.g. Exception), so I suppose I should put that <? extends Exception>
somewhere.
How can I achieve the above?
Example Test:
map.put(String.class, new ArrayList<String>()); //Fail because String is not an Exception
map.put(IOException.class, new ArrayList<FileNotFoundException>()); // Fail because FileNotFoundException is not an IOException even though it is a subclass of it
map.put(FileNotFoundException.class, new ArrayList<IOException>()); // I suppose I'm fine with this.
map.put(IllegalArgumentException.class, new ArrayList<IllegalArgumentException>()); // Succeed
map.put(NumberFormatException.class, new ArrayList<ServerException>()); // Fail again because the two classes don't match
map.put(ClientDecodingException.class, new ArrayList<ClientDecodingException.class>); // Succeed again since the map takes in a wildcard Exception
解决方案 I've done this by using raw Class
and List
(still cannot fix this) and by using a map wrapper for storing Exception
only:
public class ExceptionMapWrapper {
private Map<Class, List> myMap;
public ExceptionMapWrapper() {
myMap = new HashMap<>();
}
//relevant methods for the test: put and get
public <T extends Exception> void put(Class<T> clazz, List<T> list) {
myMap.put(clazz, list);
}
public <T extends Exception> List<T> get(Class<T> key) {
return myMap.get(key);
}
}
And a simple test for this:
ExceptionMapWrapper exceptionMapWrapper = new ExceptionMapWrapper();
Class<IOException> clazz = IOException.class;
List<IOException> list = new ArrayList<>();
exceptionMapWrapper.put(clazz, list);
//compiler errors, uncomment to see them
//exceptionMapWrapper.put(String.class, new ArrayList<String>());
//exceptionMapWrapper.put(IOException.class, new ArrayList<ClassCastException>());
//exceptionMapWrapper.put(IOException.class, new ArrayList<SQLException>());
List<IOException> ioExList = exceptionMapWrapper.get(clazz);
//compiler error, uncomment to see
//List<SQLException> sqlExList = exceptionMapWrapper.get(clazz);
这篇关于How to say“A Class of Class<?>列出< The class>"在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!