자바 혼성 컨테이너
public static <T> List<T> get(Class<List<T>> class1, Map<Class<?>, List<?>> map){
@SuppressWarnings("unchecked")
List<T> cast = (List<T>) class1.cast(map.get(class1));
return cast;
}
public void testHete() throws Exception {
Map<Class<?>, List<?>> map = new HashMap<Class<?>, List<?>>();
List<String> sList = new ArrayList<String>();
List<Integer> iList = new ArrayList<Integer>();
sList.add("a");
sList.add("b");
iList.add(new Integer(10));
iList.add(new Integer(20));
map.put(String.class, sList);
map.put(Integer.class, iList);
List<String> aa = List.class.cast(map.get(String.class));
for (String element : aa) {
System.out.println(element);
}
List<Integer> bb = List.class.cast(map.get(Integer.class));
for (Integer k : bb) {
System.out.println(k);
}
List<String> foo = Debug.<String>get(List<String>.class, map);
List<Integer> bar = Debug.get(List<Integer>.class, map);
bar.add(new Integer(30));
for (Integer i : bar) {
System.out.println(i);
}
MyClass<String> a = new MyClass<String>();
String oo = a.createContents(String.class);
BoxImpl<String> myBox = BoxImpl.<String>make();
}
public static class BoxImpl<T>{
public static<V> BoxImpl<V> make(){
return new BoxImpl<V>();
}
}
private static class MyClass<E>{
public MyClass() {
// TODO Auto-generated constructor stub
}
E createContents(Class<E> clazz){
try {
return clazz.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "HooneyWorld";
}
}