FacebookTwitter
Hatrack River Forum Post New Topic  Post A Reply
my profile login | register | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Java Collections Generic question

   
Author Topic: Java Collections Generic question
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post   Reply With Quote 
I don't think this is possible - I couldn't find it anywhere I looked - but nothing lost from asking.

Does anyone know if there is a way to get the generic type of a collection in java?

What I mean is, let's say I have a class with a property ArrayList<T> that I instantiate and set the ArrayList as say an ArrayList<String>. Is there anyway to get the String class of this array list? I figured this is impossible because the generics are erasures, so there's no way to get information about it during run time, but it'd be so much easier if I could get this work.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Xavier
Member
Member # 405

 - posted      Profile for Xavier   Email Xavier         Edit/Delete Post   Reply With Quote 
I don't believe there is any way to know at run-time.

I'm trying to imagine why you need to know what the type of the collection was to think of a work-around. Would inspecting the elements of the list to get their types not be sufficient?

You know, like:

Object o = list.get(0);
System.out.println(o.getClass());

Posts: 5656 | Registered: Oct 1999  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post   Reply With Quote 
Yep, type erasure makes it impossible in the general sense. There are, however, several options outside of that. Without knowing your particular situation, I can't know if any is appropriate.

1) Re-architect the code so this isn't an issue. Most of the time, when you find yourself wanting to do this sort of thing, it means someone did a bad job of designing some code.

2) Make sure all the possible classes are subclasses/subinterfaces with specific types that are derived from generics with specific type parameters. MyStringList extends ArrayList<String> and all that. This is hacky, but keeps things typesafe.

3) Less hacky, if you know the type information at some point, is using the type inside of another type, and parameterizing by that. That is, inside MyWrapper<ItemType> instantiate ArrayList<ItemType>.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
ricree101
Member
Member # 7749

 - posted      Profile for ricree101   Email ricree101         Edit/Delete Post   Reply With Quote 
Wouldn't that be prone to failure if it was declared with a base class, but if the first element was a child class.

In other words, if you had something like ArrayList<BaseClass>
and you inserted an instance of ChildClass,
then ran Xavier's code, what would the output of o.getClass() be. I'm not too familiar with Java, but I suspect it would be ChildClass, when you really wanted BaseClass.

Posts: 2391 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post   Reply With Quote 
I trying to do a quick set of Collections bindings. I've got a bunch of utilities and gui classes that I use with my custom designed Data Binding interfaces and I was trying to get some no fuss ways of adding and editing items with a collection.

Really, this isn't that big a deal. My other solution is to require a class to be set for the binding, because I can't ensure that the collections I dealing with won't be empty, so I can't pull it from there.

It's just that it'd be easier and cooler to just pass in a typed collection and have it know that that is the class.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
ricree101
Member
Member # 7749

 - posted      Profile for ricree101   Email ricree101         Edit/Delete Post   Reply With Quote 
You can always just throw the question up on a Q&A site like Stackoverflow and see if anything comes of it.
Posts: 2391 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post   Reply With Quote 
Yeah, I'd go with my third listing up above: parameterize the wrapper. So you'd do things like Binding<T>.
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Nighthawk
Member
Member # 4176

 - posted      Profile for Nighthawk   Email Nighthawk         Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Xavier:
You know, like:

Object o = list.get(0);
System.out.println(o.getClass());

Wouldn't work if the collection is empty.

I don't know about Java. Will have to check in .NET and maybe something can be discerned from that.

Posts: 3486 | Registered: Sep 2002  |  IP: Logged | Report this post to a Moderator
Xavier
Member
Member # 405

 - posted      Profile for Xavier   Email Xavier         Edit/Delete Post   Reply With Quote 
quote:
Wouldn't work if the collection is empty.
Well duh, but why would you need to know the type of an empty collection?

Anyway, fugu's option 3 is one I use fairly often, and I've liked the results.

Posts: 5656 | Registered: Oct 1999  |  IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post   Reply With Quote 
Knowing the type of the empty collection is very important if you have a binding to add a new item to a collection.

The main thing I need this for is generating new objects of the collection type, so I decided to go with putting a class property on the new item binding.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Alcon
Member
Member # 6645

 - posted      Profile for Alcon   Email Alcon         Edit/Delete Post   Reply With Quote 
Just a tip - if you need an answer to any programming related question, go here: www.stackoverflow.com.

If you can't find a question similar to yours then you can ask your question and get an answer in a matter of minutes.

Edit: And apparently I'm slow and didn't see that someone else had already posted it. In any event - I second the suggestion to ask Stack Overflow.

Posts: 3295 | Registered: Jun 2004  |  IP: Logged | Report this post to a Moderator
   

Quick Reply
Message:

HTML is not enabled.
UBB Code™ is enabled.
UBB Code™ Images not permitted.
Instant Graemlins
   


Post New Topic  Post A Reply Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2