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
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:
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..
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.