1

Please a take look at example code below:

Object o1= new Integer(4); 
ArrayList<Integer> list=new ArrayList<>();
list.add((Integer) o1);

Instead I'd Like to do something like:

Object o1= new Integer(4); 
ArrayList<o1.getClass().getSimpleName()> list=new ArrayList<>();
list.add((o1.getClass().getSimpleName()) o1);

o1.getClass().getSimpleName() returns "Integer" as a Java.lang.String object, my question is how can I embed this string into my code some how with reflection so the type of items in list can be determined at runtime. It is possible to do so by switch statement like:

Object o1= new Integer(4); 
switch(o1.getClass().getSimpleName()){
    case "Integer":    
        ArrayList<Integer> list=new ArrayList<>();
        list.add((Integer) o1);
    case "String":
        /* some code */
}

but I hope there will be a better solution.

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
S.Rad
  • 307
  • 2
  • 10
  • 1
    What advantage are you hoping to get from this? During runtime everything is basically `List` anyway. – Marvin Jun 25 '17 at 21:59
  • @Marvin I'm using jflex scanner generator and cup parser generator to make a compiler front end, for type checking I'm using a hashmap to keep track of identifiers and their types,I believe vectorizing identifiers of the same type can be used to improve the speed of type checking though not dramatically and, I was really curious about this reflection thing too:) – S.Rad Jun 25 '17 at 22:49
  • The fundamental mistake you are making is that you are mixing compile-time and runtime concepts. Type parameters are purely a compile time thing - they have to be known at compile time. You cannot do something like `ArrayList`, the method call is something that will be evaluated at runtime, not at compile time. – Jesper Jun 26 '17 at 06:25
  • Thank you @jasper – S.Rad Jun 26 '17 at 06:59

2 Answers2

0

It is not possible due to type erasure. What you can do though is to have a list like this:

List<Object> list = new ArrayList<>();
list.add(12);
list.add("String");

So this way you can have whatever object you want in your list

Mibac
  • 8,990
  • 5
  • 33
  • 57
  • According to http://docs.oracle.com/javase/tutorial/java/generics/erasure.html there is no way to use reflection like I intended. thank you – S.Rad Jun 25 '17 at 22:51
0

As @Mibac said, you can't create list this way.

What you can do is somekind of "casting" using Stream API, if you are 100% sure that all objects int your list are of the same type:

Object o1 = new Integer(2);
List<Object> someList = new ArrayList<>();

someList.add(o1);

List<Integer> anotherList = new ArrayList<>();
anotherList.addAll(someList.stream().map(o -> (Integer) o).collect(Collectors.toList()));

System.out.println(anotherList);
Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
  • I might be wrong but can't you just cast it? (`(List) someList`) – Mibac Jun 25 '17 at 22:17
  • Nope. It's impossible. Generic types cannot be casted such way. It produces compile time error `Error:(61, 39) java: incompatible types: java.util.List cannot be converted to java.util.List` – Maciej Treder Jun 25 '17 at 22:20