aspose file tools*
The moose likes Java in General and the fly likes Question on generic methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on generic methods" Watch "Question on generic methods" New topic
Author

Question on generic methods

Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
A question about generic methods from this tutorial.
http://java.sun.com/docs/books/tutorial/extra/generics/methods.html
It says that the below method can be called with any kind of collection whose element type is a supertype of the element type of the array. My question is how is this specified without using the super keyword. As per my understanding, T is a type, array and the collection are both specified to take elements of type T.



Thanks,
Devi
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Example:
If I understand your question correctly, the method will add all members of the array to the collection. As long as the collection is declared with a type-compatible class of the array, the method will work.

Does this help?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I'm certainly no expert on generics but I'll tell you what understand by that description (and by looking at the examples in the tutorial).

Suppose X and Y are types and X is a supertype of Y (write X > Y ).
Then we know that X[] > Y[].

Suppose we have reference variables ArrayList<X> x and Y[] y, and suppose we call fromArrayToCollection(T[] a, Collection<T> c) like this:

fromArrayToCollection(y, x); //

The question is what is T, the type parameter inferred to be?

It is X, the supertype. So inside the method T is X. So the method really looks like (read the comments):

So basically this is saying that you can convert an array of Ys (which is an array of Xs) into an ArrayList of Xs.

I feel rather dizzy, so please feel free to pick holes in my interpretaion.
-Barry
[ September 23, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
I'm sorry but I'm still not clear.
The method is
fromArrayToCollection(T[] a, Collection<T> c)

If you look at the implementation, then its clear that since the array elements that are added to the collection, the collection element type can
be the supertype of the array element type.

But just by looking at the declaration, doesn't it specify that the array element and Collection element are of the same type T ? Should we not say fromArrayToCollection(T[] a, Collection<? super T> c)

Thanks
Devi
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
It does work. But how does it work without the super keyword.
Shouldn't the method definition be as below ?

I'm really confused. Any inputs would be welcome.

Thanks
Devi
[ September 24, 2005: Message edited by: Sreedevi Vinod ]
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
The compiler makes that inference. that is how it works. I hope that answers one of your questions.
Whether it should be as you said (which is also right) or not is a question you should ask the author of the class.
As far as I can see, the original code looks more compact that the newer version you have given + does the same work, so maybe thats why it was kept.


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
In case you wanted to ask why this works, note that both of these have the same erasures.
both of these translate to (I think so, please correct me if this is wrong)
fromArrayToCollection(Object[] a, Collection c)
for(Object o: a)
c.add(o);
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sreedevi, if you would like me to move this thread to Java In General (Intermediate), I will do so. Perhaps you will get more help there.
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
Thanks Akshay, Barry and Steve for your replies.
I think its somewhat clear now, Barry.
But still it would be helpful to receive more inputs.
So please do move it to the Intermediate forum.

Many Thanks
Devi
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Will do...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sreedevi Vinod:

But just by looking at the declaration, doesn't it specify that the array element and Collection element are of the same type T?


Exactly!

The trick is that an array variable of type T[] can hold a reference to an array of type S[] as long as S is a subtype of T.

That is, if you call

fromArrayToCollection(new X[]{...}, new ArrayList<Y>());

the method gets called as

fromArrayToCollection(Y[], Collection<Y>

which just happens to work as intended.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ilja,
why is this type inference done?
Why doesn't the compiler just simply reject the call saying that the collection type parameter is not exactly the same as the array type?

That is: allow the call if parameters are Integer[], ArrayList<Integer> , but reject call if paramters are Integer[], ArrayList<Number>. The programmer has specified that he wants the former, not the latter.

As Shreedevi mentioned the programmer can specify the latter behaviour as:
T[], ArrayList<? super T> so why is the compiler being so "helpful" in the case of T[], ArrayList<T>?
[ September 25, 2005: Message edited by: Barry Gaunt ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Well - that's the idea of inheritance in java.

If a method accepts a parameter of type T (number), it will allow every subtype of T (Integer).

Having an array or a Collection of Ts doesn't make a difference.


http://home.arcor.de/hirnstrom/bewerbung
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Like Stefan said.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078

That is: allow the call if parameters are Integer[], ArrayList<Integer> , but reject call if paramters are Integer[], ArrayList<Number>. The programmer has specified that he wants the former, not the latter.


Because the former is the latter by definition if the former is a subtype of the latter?
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
Originally posted by Ken Blair:


Because the former is the latter by definition if the former is a subtype of the latter?


Ken don't be confused, Barry was citing that "it doesn't work that way" as you're thinking it does.
fromArrayToList(Integer[], ArrayList<Number> actually works and that is the whole point that is being mooted.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Akshay Kiran:


Ken don't be confused, Barry was citing that "it doesn't work that way" as you're thinking it does.
fromArrayToList(Integer[], ArrayList<Number> actually works and that is the whole point that is being mooted.


Don't be confused? I have no idea what you are trying to say with this post. I know it works, Barry seemed to be asking Ija why it works and implying that it shouldn't. Are you saying he was just playing devil's advocate and I missed the sarcasm?
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
hehe yeah, it wasn't clear whether you were trying to read Barry's mind or asking a seperate question altogether. I just thought I should dispel doubt (albeit nonexistent) because someone reading that would think you're making a point there sorry about that.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on generic methods