• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly.

Stated differently:
Generics are a compile-time construct. They don't exist at run time.
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic