argument « Generic « Java Collection Q&A

Java Collection Q&A
3.Array Byte
4.Array Char
5.Array Convert
6.Array Dimension
7.Array Integer
8.Array Object
9.Array String
14.Garbage Collection
Java Collection Q&A » Generic » argument 

1. Generic method in Java without generic argument

In C# I can do actually this:

//This is C#
static T SomeMethod<T>() where T:new()
  Console.WriteLine("Typeof T: "+typeof(T));
  return new T();

//And call the method here
But for some reason I can't get ...

2. generics error: not applicable for the arguments

Can someone explain to me why the following code does not work?

public class Test {

 interface Strategy<T> {
   void execute(T t);

 public static class DefaultStrategy<T> implements Strategy<T> {

3. Why can't I use wildcard with methods receiving a parameterized argument?

For example, I use a method Measure.doubleValue(Unit<?> unit) which returns the double value of a measurement, expressed in the specified Unit. If I pass a Unit<?> variable to it, I get ...

4. What would be the use of accepting itself as type arguments in generics

I saw some code on an unrelated question but it got me curious as I never saw such construct with Java Generics. What would be the use of creating a ...

5. Java-syntax for explicitly specifiying generic arguments in method calls

What is the syntax for explicitly giving the type parameters for a generic Java method?

6. Generics: Compiler seems incapable of recognizing that the type passed to an argument is the same as what is returned - why?

Let's say I have several POJOs which all extend a common supertype, BaseObject. I have a GenericDao which is declared as public interface GenericDao<T>. For each type-specific DAO, I have an interface ...

7. Is it possible to get class of the generic argument?

I'm kind of lame with generics, but I wonder, for the following class:

static class SomeClass<T> {
 private T value;

 public SomeClass(T value) {
  this.value = value;

 public ...

8. Limiting type arguments for generics in C#

I could very will be imagining things, but I seem to recall in Java that I can declare a field or parameter as such:

public class BarHandler{

    public Class<? ...

9. How can I learn actual type argument of an generic class?

I have a parameterized class : class ParameterizedClass<T extends AbstractSomething> { } calling: new ParameterizedClass<Something>(); So how can I get actual type Something of T by using Java Generics?

10. multiple nested wildcard - arguments not applicable

I've heavily simplified my problem. Here's how it reads. I'm trying to figure out why the following code does not compile:

 List<AnonType<AnonType<?>>> l = new ArrayList<AnonType<AnonType<?>>>();
 l.add( new AnonType<AnonType<String>>() );
public class AnonType<T> ...

11. Retrieving the Class object of the generic argument

I am not sure if this is a duplicate question to this one mainly because I'm a bit new to generics, so please be forgiving. I have a generic class:

public class ...

12. With Java, is it acceptable to use an unbounded wildcard type as a method's argument, and then check and cast it to a parameterized type?

If I use unbounded wildcard types for two collections (each collection will have a different type) as the arguments for a method:

private void doAssertion(List<?> testList, List<?> generatedList)
Inside this method, can I ...

13. A java method that has a generic parameter- why can't I pass an object with a generic parameter that is a subclass of the method arguments?

What I mean is, in the code below:

class base {


class derived extends base {


class WTF {

    public void func(List<base> list) {



14. How do java generic methods constrain the method type arguments?

I've been reading about generic methods, and thought I understood how the generic type argument would constrain the method parameter types, but when I tested out some of the ideas with ...

15. The method read(capture#2-of ?) in the type GenericDao is not applicable for the arguments (Long)

First time doing generics and I am a bit confused here. I have the following:

public interface GenericDao<T, PK extends> {

     * Reterive an ...

16. How do nested type arguments work?

Why does the declaration

Set<Set<String>> var = new HashSet<Set<String>>();
work but the declaration
Set<Set<String>> var = new HashSet<HashSet<String>>();
choke? I'm aware that 'top level' (not sure if that's the correct phrase here) generics in a declaration ...

17. How to get the actual type arguments to an indirectly implemented generic interface?

I have a parameterized interface that is implemented in many different ways. At run time I need to figure out, given an arbitrary object that implements that interface, what the actual ...

18. Help with Java Generics: Cannot use "Object" as argument for "? extends Object"

I have the following code:

import java.util.*;

public class SellTransaction extends Transaction {
    private Map<String,? extends Object> origValueMap;
    public SellTransaction(Map<String,? extends Object> valueMap) {

19. java interface input argument extends from a base class

I wants to create an interface that allows any implementation class like this:

public interface MyInterface

   void doSomething( <A extends MyBaseClass> arg1);

public class MyImpl implements MyInterface
  void doSomething ( ...

20. Generic type for argument that is also a return value

I have a non-generic code like this:

class List {
  List intersect(List out, List other) {
    if (out == null) out = new List();
    // ...

21. How to pass a parameterized class as an argument

My goal is to develop a class that can output an object of a specified class.

public class GetMe<T> {
    public T get() {

22. How can I pass a generic class to a method in Java?

Suppose we have a function that creates objects given a particular class:

public static <T> T createObject(Class<T> generic) {
    try {
        return ...

23. How to return a generics type without explicit specification of its argument?

This is the Java 1.6 interface:

interface Foo<T extends Foo<T>> {
This is a factory, which is supposed to return "Foo<Foo>":
interface Factory {
  <T extends Foo<T>> Foo<T> find();
Can't compile the factory class:
incompatible ...

24. Why is a generic argument that "extends" something not allowed in derived function, but generic return type is?

I would like to override a generic function in a subclass, as follows: Superclass:

public abstract class Metric {

    public abstract <T extends Foo> T precompute(); // valid syntax

25. Is it okay to pass an Object object as method argument

I was writing a quick method that is using Google Gson to translate an Object to JSON. It's simple, but I wanted to centralize my JSON building process in case I ...

26. Access generic argument of the return type

Is it possibly to access the generic argument in the following line ?

 public List<StoryLikeRef> getLikes() throws IOException
I mean by reflection get the StoryLikeRef from out the return type ? Thanks

27. How can I merge three similar methods with different argument types into one generic one?

I am relatively inexperienced with java & generics, so please excuse me if this is a stupid question. I have 3 very similar helper methods called verifyTextualSort, verifyNumericSort and verifyDateSort. The ...

28. Generics: Creating parameterized class argument

How does one get a parameterized Class object to be used as a method argument?

class A<T>
    public A(Class<T> c)


29. What type parameter will be used if I call a generic method with a raw type as argument?

I'm just curious what's the generated inferred T type when passing a raw to a T type. It does compile but with a warning.

public class GenericMethodInference {

    static ...

30. Java generics compilation error - The method method(Class) in the type is not applicable for the arguments

Last Thursday someone at work showed me a compile error that I wasn't able to fix in a clean way and it has been bothering me ever since. The problem is generics ...

31. Java Generics: Method X is not applicable for the arguments

public abstract class EventHandler<E extends EventArgs> { 
    public abstract void HandleEvent(Object sender, E e);
public class Observers<E extends EventArgs> {

    private CopyOnWriteArrayList<EventHandler<? extends ...

32. Accessing type arguments from generic types used in typebounds

I have discovered the following problems with generics. Consider the generic interface

public interface A<X> {
    X get();
    void doStuff(X x);
Now, let's assume the following method ...

33. java generics integer value argument

thanks Nepomuk but not exactly what i am looking for... for example i want to make a generic Vector where the user will use it as: Vector<2> v1; Vector<3> v2; this ...

34. Generics "is not applicable for the arguments"

I am starting to get into the world of generics. My first attempts have not been very successful. Here is some simple code I'm tying to compile: public static final void main (String [] args) { class Shape { } class Box extends Shape { } LinkedList list = new LinkedList(); list.add(new Box()); } I'm using Eclipse 3.1 and ...

35. How to call a generic method with two bounds (for a single argument) if one bound needs a cast

I want to specify two types for a method's formal parameter. That's still simple: Make it a generic method with a type parameter like . Calling that method having an expression of type MySubClass (which extends MyClass and implements MyInterface) works fine. But how can i call the method if i have an expression of (compile-time) type ...

37. Generic problem in passing arguments ( question )

That will never work, at least not in Java. You're never returning the value at all, so outside the method it never changes from its initial value, which is null. Read up on the difference between "pass by value" and "pass by reference", and consider the fact that Java is ALWAYS pass by value.  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.