generics

Useful example with super and obscurity with extends in Generics?

折月煮酒 提交于 2020-01-09 10:12:09
问题 I know that there are a lot of questions about this topic, but unfortunately they couldn't help me to eliminate my obscurities. First of all, look at the following example. I don't understand, why the following "add"-method someCage.add(rat1) doesn't work and aborts with the following exception: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(capture#2-of ? extends Animal) in the type Cage is not applicable for the arguments (Rat) Is this the same

How to set the generic type of an ArrayList at runtime in java?

社会主义新天地 提交于 2020-01-09 08:13:31
问题 Ok, so I am setting up my own XML serialization (I know there are others out there, even some built in to Java, but I am doing it myself to learn and because it is so awesome to work with). I have serialization down. I am currently on the deserialization (reading in the XML file and assembling objects based on the data in the file) and I am running into problems with setting generic types. After extensive research, I figured out how to get the generic types of a class so I could write them

How to set the generic type of an ArrayList at runtime in java?

亡梦爱人 提交于 2020-01-09 08:09:08
问题 Ok, so I am setting up my own XML serialization (I know there are others out there, even some built in to Java, but I am doing it myself to learn and because it is so awesome to work with). I have serialization down. I am currently on the deserialization (reading in the XML file and assembling objects based on the data in the file) and I am running into problems with setting generic types. After extensive research, I figured out how to get the generic types of a class so I could write them

Generics and inheritance: need a complex Map instance

て烟熏妆下的殇ゞ 提交于 2020-01-09 07:58:07
问题 public abstract class Mother { } public class Daughter extends Mother { } public class Son extends Mother { } I need a Map which keys are one of Daughter or Son classes, and which values are lists of objects of one of those two classes, respectively . For example: /* 1. */ map.put(Daughter.class, new ArrayList<Daughter>()); // should compile /* 2. */ map.put(Son.class, new ArrayList<Son>()); // should compile /* 3. */ map.put(Daughter.class, new ArrayList<Son>()); // should not compile /* 4.

Creating generic array in Java via unchecked type-cast

我的梦境 提交于 2020-01-09 06:48:07
问题 If I have a generic class Foo<Bar> , I am not allowed to create an array as follows: Bar[] bars = new Bar[]; (This will cause the error "Cannot create a generic array of Bar"). But, as suggested by dimo414 in an answer to this question (Java how to: Generic Array creation), I can do the following: Bar[] bars = (Bar[]) new Object[]; (This will "only" generate a warning: "Type safety: Unchecked cast from Object[] to Bar[]"). In the comments responding to dimo414 's answer, some people claim

how to instanceof List<MyType>?

风流意气都作罢 提交于 2020-01-09 06:14:07
问题 How can I get this sort of thing to work? I can check if (obj instanceof List<?>) but not if (obj instanceof List<MyType>) . Is there a way this can be done? 回答1: That is not possible because the datatype erasure at compile time of generics. Only possible way of doing this is to write some kind of wrapper that holds which type the list holds: public class GenericList <T> extends ArrayList<T> { private Class<T> genericType; public GenericList(Class<T> c) { this.genericType = c; } public Class

how to instanceof List<MyType>?

随声附和 提交于 2020-01-09 06:13:14
问题 How can I get this sort of thing to work? I can check if (obj instanceof List<?>) but not if (obj instanceof List<MyType>) . Is there a way this can be done? 回答1: That is not possible because the datatype erasure at compile time of generics. Only possible way of doing this is to write some kind of wrapper that holds which type the list holds: public class GenericList <T> extends ArrayList<T> { private Class<T> genericType; public GenericList(Class<T> c) { this.genericType = c; } public Class

Is this valid Java?

六月ゝ 毕业季﹏ 提交于 2020-01-09 04:13:19
问题 Is this valid Java? import java.util.Arrays; import java.util.List; class TestWillThatCompile { public static String f(List<String> list) { System.out.println("strings"); return null; } public static Integer f(List<Integer> list) { System.out.println("numbers"); return null; } public static void main(String[] args) { f(Arrays.asList("asdf")); f(Arrays.asList(123)); } } Eclipse 3.5 says yes Eclipse 3.6 says no Intellij 9 says yes Sun javac 1.6.0_20 says yes GCJ 4.4.3 says yes GWT compiler says

Java 8: Reference to [method] is ambiguous [duplicate]

这一生的挚爱 提交于 2020-01-09 03:43:06
问题 This question already has answers here : Java type inference: reference is ambiguous in Java 8, but not Java 7 (2 answers) Closed 4 years ago . Does anybody understand why the following code will compile fine in Java 7 and below, but fails with Java 8. public static void main(String[] args) throws Exception { put(get("hello")); } public static <R> R get(String d) { return (R)d; } public static void put(Object o) { System.err.println("Object " + o); } public static void put(CharSequence c) {

Implementing arithmetic in generics?

China☆狼群 提交于 2020-01-09 03:41:08
问题 Is it possible to implement basic arithmetic (at least addition) in C# generics, like you can with C++ templates? I've been trying for a while to get them up and working, but C# doesn't let you declare the same generic type multiple times, like you can with templates. Extensive googling did not provide an answer. EDIT: Thanks, but what I'm looking for is a way to do the arithmetic at compile time, embedding something like Church numerals in generics types. That's why I linked the article that