aspose file tools*
The moose likes Beginning Java and the fly likes how to print only strings using generics when the list is having multiple types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to print only strings using generics when the list is having multiple types" Watch "how to print only strings using generics when the list is having multiple types" New topic
Author

how to print only strings using generics when the list is having multiple types

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

i have the following code where i have many type values in the arrayListbut i wanted to print only those values which are strings using Generics ( i dont want to put any if condition in the while loop which checks for type) Is it possible ..i tried but below code did not help



thanks
Tanu Gulati
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

I don't think it is possible to do so.(I am not sure why you want to avoid if check, but better option would be to put instance of check in while loop using if)


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Generics don't work that way. They will not "filter" elements of an existing List. In fact, when you compile this code, the compiler will generate warnings about "unchecked or unsafe operations." This is because you are misusing generics -- especially with the unchecked conversion:

List<String> list = arrayList;

Properly used, generics would prevent you from adding a non-String to the List in the first place.

See Java Tutorial - Generics, and especially note Type Erasure.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

i got it .....it is at the time of creation of a list when generics come to play but type check dont apply on already created list..

thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Originally posted by Tanu Gulati:
i got it .....it is at the time of creation of a list when generics come to play but type check dont apply on already created list..

thanks
Correct.

If you have a non-generic List you can use the instanceof operator to check whether you have a String when you retrieve the contents of the List, but a List of different class types looks like a recipe for disaster to me.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Exactly.

Stated differently:
Generics are a compile-time construct. They don't exist at run time.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to print only strings using generics when the list is having multiple types