Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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: 76075
362
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic