Should a future release of Java deprecate the use of raw types to force the migration to generics? I could also see having raw types not allowed by default but allowing ...
I would like to hear from you guys on how do you decide when you should be using concrete parameterized type vs. bounded parameterized type when designing API, esp. (that I ...
public class Sequence<T> {
protected List<T> sequence = new ArrayList<T>();
public Matrix<OrderedPair<T, ?>> createCartesianProduct(Sequence<?> secondSequence) {
Matrix<OrderedPair<T, ?>> result ...
Is there any Java compiler flag that one can pass to tell the compiler to disallow the use of raw types? That is, for any generic class, let the compiler force ...
I have an interesting situation and I'm wondering if there is a better way to do this. The situation is this, I have a tree structure (an abstract syntax tree, ...
The Java type system supports only invariant types. So a List<String> is not an List<Object>. A List<String> is not a List<Object> as it is not valid to insert an Integer into ...
I was trying to write a generic class, that could operate on any simple type (int, float, etc.). I wanted to avoid repeating the same function several times, with changing the ...
OK - I know that Java generics can be a minefield for the unwary, but I just came across a non-intuitive (to me anyway) behavior that I was wondering if anyone ...
allows arbitrary number and types of parameters to be used.
I'd love to use generics for strict definition of the number and types of parameters.
For example, Let's assume ...
I'm currently developing a lightweight, general-purpose simulation framework. The goal is to allow people to subclass the Simulation and Scenario objects for their domain-specific needs. Generics seemed like an appropriate way ...
After using C++ I got used to the concept of Identifier which can be used with a class for the type, provides type safety and has no runtime overhead (the actual ...
Generics in Java is noisy sometimes. The parameterized types are thrown out by the compiler anyway after compile, so my question is, is there really any drawbacks in ignoring them, as ...
public class FooImpl implements Foo { /* ... */ }
public class Main {
public static <T> Collection<T> getList(Class<? extends T> itemClass) { /* ... */ ...
import java.util.List;
import java.util.ArrayList;
public class TypeTest {
public static class TypeTestA extends TypeTest {
}
public static class TypeTestB extends ...
I'm wondering if it's possible to de-serialize a generic type into an instance of that type. If it is possible, does Java take into account the generic type's custom de-deserialization (if ...
my question is about type variables used in generic classes and methods,
why can't we do something like this T = new T(); "Construct an object of the type variable"
i know that ...
For a CS class I need to solve an assigned problem using three data structures: Queue, PriorityQueue, and Stack. I wanted to write a single solution to the problem using ...
I've been reading Effective Java and decided to try to put some of what I've learned into action. I'm trying to effectively create a Multimap<?, Condition<?> > where the ...
I remember to have read about a new feature of JDK7 (now also supported by the Netbeans editor, yet only in current trunk builds).
This tiny feature wouldn't produce a warning anymore ...
I would like to make a generic function that deserializes a byte array into a type specified by the caller.
I feel that a really intuitive way to call such a function ...
I am trying to create a generic class for use with Google Gson. I've created the class GsonJsonConverterImplementation<T>. This class has the following method:
I'm trying to compare two subclasses of Number inside a class with generics. In the code below, I'm trying to compare Number objects inside an instance of Datum.
How do I enforce ...
I'm trying to create a generic class that accepts only two types (i.e., Integers and Doubles), as if I would make the class with only Double type, I would be wasting ...
import java.util.*;
class Foo<T> {
public int baz(List<String> stringlist) { return 1; }
public int baz(ArrayList<Object> objectlist) { return 2; }
public static void main(String[] args) {
...
I am trying to understand generics and I purposely want to generate a classcastexception, however, i instead get an arraystoreexception on the first attempt.
Java's generics would erase the type information after the source code was compiled. And i guess the "erase" is necessary because java only keep one copy of class no matter what ...
Given the following situation it's only possible to write a model handler which works on SpecificModel, it's not possible to have a model handler which handles only models. But when we ...
Lets assume I want to have a class that acts a descriptor for records, where each record has a set of attributes.
Each attribute has a unique name and should have a ...
suppose I'm trying to write a function to return an instance of the current type. Is there a way to make T refer to the exact subtype (so T should refer ...
I am reading the chapter on Generics in Effective Java.
Help me understand difference between Set, Set<?> and Set<Object>?
The following paragraph is taken from the book.
I'd like to use define a generic class in java, that can only be instantiated using my custom data types that all share the same base class. Is it possible to ...
I want to deserialize a JSON object (using GSON, because I already use it for searializing objects) to a general Map of type Map<String, Object>. It should create Objects of types ...
I'm writing a Java wrapper for c++ and would like to use a generic class to wrap a c++ template. Therefore I'd like to get the generic type as String so ...
In the code below, I give two main classes - TestWorks, and TestCompilesButFails. I'm not sure I understand the failure - it would appear that the Arrays.asList() expression is being given ...
ArrayList<Integer> arrI = new ArrayList<Integer>();
ArrayList arrO = arrI; // Warning
/* It is ok to add a String as it is an ArrayList of Objects
but the JVM ...
Trying to create a static factory method of a parameterized type to be passed to the class of the objects that the factory is creating, but I'm unable to access the type parameters (compile-time error) because the method is static. Is it possible to pass the type parameter to the static method? The following source attempts to demonstrate what I'm trying ...
No, you can't. Generics are meaningful only at the source code level. They are not preserved in the class file. So there is no way by reflection to get information about generics from the compiled class. This is called "erasure." The reason for it is to preserve binary compatibility with older compiled code that does not use generics. Geoffrey
Map.Entry is an interface, hence, Map.entrySet() actually implements a new Map.Entry. Then, you're a actually receiving extends Map.Entry> and not Map.Entry itself. Actually, reviewing the Tiger source code I discovered that HashMap has static nested class named Entry which hides that declard in the Map interface. This static nested class named Entry implements Map.Entry. At least thats what I think. ...
If you're referring to not having to cast to/from the Object then no. In order to use generics they must have a common type and casting to/from that type is the only thing you can avoid. I.e. You can't do Color c = vector.get(1) with a Vector
I'm trying to create a generic map with type specified at runtime and its being a pain. The following does not compile: final Type type = ... some type or class final Map map = new HashMap(); I've tried replacing the Type reference with Class, but no luck. The compiler does not recognize type in . Any suggestions? If there isn't ...
Hi I was trying out some generics in my code. I understand that i cant get away without a warning if i dont specify the type of the collection in 1.5. This is my code and it gives a warning that reads like "Type safety: The expression of type List needs unchecked conversion to conform to List". List vehicleYearList = new ...
Dear guys, I have declared LinkedList LL = new LinkList (); in my main class. Under my class Customer, I have a compareTo method too. I get an error on Type parameter customer is not within its bound. I believe the problem lies with the bold portion of the code which sorts the node. Can somebody guide me please? Thank in ...
I have written the following code , to check what is the incoming object and add it in the collection but, i get an error given below class Employee{ } class BankAcount{ } class GenericClass{ UseT1 t1; UseT2 t2; ArrayList al; GenericClass(UseT1 t1,UseT2 t2){ this.t1 = t1; this.t2 = t2; } public UseT1 getT1() { return t1; } public void setT1(UseT1 ...