Menu
  • HOME
  • TAGS

Java generic usage error

java,collections,java-generics

You're using the raw IValue type in your DelayQueryKey class definition class DelayQueryKey<T> implements Delayed, IValue { DelayQueryKey<String> therefore doesn't fit the bounds set out by McDelayQueue<E extends Delayed & IValue<K>, K> Instead, parametrize it (appropriately) class DelayQueryKey<T> implements Delayed, IValue<T> { Although not necessary in this case, develop the...

Exception with generic set of ConstraintViolations

java,generics,java-generics

You can't extend an exception with generics: http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#cannotCatch

How can I implement generic method which has generic return type and generic parameter?

java,generics,java-generics

A generic is defined in the same way you have that example there, just have a placeholder for the type name. If you are looking to define it to have a generic input parameter type and a different generic return type then you can comma separate them. public <T, R>...

Sort a set and assign the content to a List in java

java,interface,implements,java-generics

This should be you are looking for: I think this will create a list and sort it. public class Ordina{ public static List<RisorsaWeb> perCriterioUtente(Set<RisorsaWeb> unstorted, Comparator<RisorsaWeb> comparator){ List<RisorsaWeb> list = new ArrayList<RisorsaWeb>(); list.addAll(unsorted); Collections.sort(list,comparator); return list; } } Note: writed only with javadoc so not tested....

How to annotate hibernate collections with generic types?

java,hibernate,jpa,hibernate-mapping,java-generics

The generic information is lost at compile-time due, to type erasure so Hibernate can't figure out what type T is in your case. You can change your code to this: private List<ReferenceRange<T>> otherReferenceRanges; And then use inheritance so that each ReferenceRange subclass embeds a certain type. public abstract class ReferenceRange<T>...

Why there is no ArrayStoreException thrown in below code?

java,arrays,generics,java-generics

In Java, generic parameterizations do not have a class which distinguishes them from each other: ArrayList<Exception> : class is ArrayList ArrayList<String> : class is ArrayList This is because generics are implemented with type erasure, which means that during compilation, they are replaced with casting: ArrayList<Exception> list = new ArrayList<Exception>(); list.add(new...

How to sum values in a Map with a stream?

java,java-8,java-stream,java-generics

You can do this: int sum = data.values().stream().mapToInt(Number::intValue).sum(); ...

Can't figure out how to instantiate generic class returned from function with condition like

java,android,generics,android-studio,java-generics

You're using the raw type Class. Class c = GiftdServiceLoader.getClassForInterface(APIAsyncLoaderTask.class); All generatic types and generic type parameters in any method invoked on such a reference are erased. Parameterize it correctly Class<APIAsyncLoaderTask> c = MyServiceLoader.getClassForInterface(APIAsyncLoaderTask.class); Note that you should use only the interface type if you aren't sure of what you'll...

How to restrict a class from taking certain types in Java?

java,java-generics

I think you want something like this: GenericOrder: public class GenericOrder<T> { private List<T> orderItems; public GenericOrder() { orderItems = new ArrayList<T>(); } public void add(T item) { orderItems.add(item); } } Let's define an interface that will be the only type that ComputerOrder allows: public interface AllowableType { } ComputerOrder:...

How do I make a list of several kind of object if I cannot use wildcards?

java,bounded-wildcard,java-generics

Wild cards is not appropriate for this use case. You should simply do List<Parent> list = new ArrayList<>(); and cast the return value from get: ChildOne co = (ChildOne) list.get(0); ChildTwo ct = (ChildTwo) list.get(1); ...

Erasing Collections generics and conflicting overloads

java,generics,type-erasure,java-generics

Both of them erase to public static void removeDuplicated(Collection, Collection). The entire parameterization is erased. You will need to give them different names. From 4.6. Type Erasure: Type erasure is a mapping [...] to types (that are never parameterized types or type variables). Type erasure also maps the signature of a constructor or method...

Eclipse tells me that Long is not Comparable

java,types,parametric-polymorphism,java-generics

public class LogBookRecord<Long, String> extends Pair<Long, String>{ ^ ^ | | generic type variable declaration (new type names) | generic type arguments That code is equivalent to public class LogBookRecord<T, R> extends Pair<T, R>{ You've simply shadowed the names Long and String with your own type variable names. Since T...