0
import java.util.*;
public class Test{
public static void main(String[] args){
  List<Integer> list=new ArrayList<>(); 

  addToList(list);     

}
static void addToList(List<?> list){
list.add(new String());
  }
}

This program gives error at list.add line. Is there someway we can modify the addToList method's body and make this program work? I want to check the Generic type of List being passed to this method. If the list being passed is of type String, then I want to add a new string to the list. Is there any way to put such condition inside the addToList method?

P.S.- Advance apologies to moderators for putting this silly question. I know the solution would be something very simple, but haven't been able to find out despite a quick google search. Please bear with me and post any quick possible solution. Thanks in advance! :)

shanti
  • 270
  • 1
  • 4
  • 20
  • Just wondering, why are you adding a string to a list of integers? – Bennett Yeo Jul 04 '15 at 05:31
  • @KiroYakuza: This is just a sample program. I just want to understand if we can somehow check for the Generic type. For now, assume that the method addToList is in a separate class, and that class has no idea what kind of list will be passed in that method. And assume that in a particular case, I would like to add a String to the incoming list. – shanti Jul 04 '15 at 05:34
  • 1
    oh so you want to check the type and then determine whether it should be added to the list. Try using the keyword `instanceOf` ( http://stackoverflow.com/questions/7526817/use-of-instance-of-in-java) If you want to check the type of objects that are in the lists you would have to use `reflection` (http://stackoverflow.com/questions/1942644/get-generic-type-of-java-util-list) – Bennett Yeo Jul 04 '15 at 05:36
  • didn't want to use instanceOf way. Purpose was to check the Generic type in some way and then take some action. Had a look at the reflection link you gave. Thanks for that. That Seems appropriate to the problem. But I was wondering, isn't there some easier way to achieve the same? :/ – shanti Jul 04 '15 at 05:48
  • Perhaps you could check whether the object exists in the list with .contains(object o). Take a look at the Java online api guide: http://docs.oracle.com/javase/7/docs/api/java/util/List.html#contains(java.lang.Object) – Bennett Yeo Jul 04 '15 at 05:50
  • No, it's not possible. If the goal is o add to the list, the type of the list shouldn't be List>. There's a design problem here. – JB Nizet Jul 04 '15 at 05:51
  • @KiroYakuza .contains(object o) ?? Can you please give the appropriate if condition that I need to put there, which could somehow check if the incoming list contains String types? – shanti Jul 04 '15 at 05:57
  • @JBNizet Are you sure it's not possible. I mean please don't focus on the sample program given. The intention is just to safely add a string to a list, after properly checking that the incoming list is of Type String (or even if type is not String, just to be able to add it at runtime) – shanti Jul 04 '15 at 06:01
  • Yes, I'm sure. See the duplicate question. – JB Nizet Jul 04 '15 at 06:05

0 Answers0