Java : How to get type of List [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Get generic type of class at runtime
23 answers
I'm working on reflection in java.
I know this is a common question and there are a lot of articles about it but I'm a bit confused and can't seem to find the right solution for what I'm trying to achieve.
I have :
public class ClassA
private List<?> myList;
// getters, setters
"myList" will contain a list of objects (DTOs) :
List<?> myList; // could be List<DTO1> or List<DTO2> ...
I'm trying to get the type of the list from another class like this :
public class ClassB
public void myMethod()
// get type of ClassA.getMyList()
I can only get "ArrayList" when I try using stuff like getClass(), getType()...
Trying to get the class of an element in the list results in "LinkedHashMap".
I really want to achieve this by using a wildcard type for the list.
So is there a way I can get the type of myList with minimal code ?
EDIT :
Thank you all for your answers.
Just one thing : myList
contains elements of type LinkedHashMap
, this wasn't very clear on my post.
It seems this really can't be done. Each LinkedHashMap
represents an object but you can't know which one. They are just a list of keys + values.
java generics reflection wildcard
marked as duplicate by Matthieu Brucher, user6910411, Max Vollmer, KevinO, Zephyr Nov 19 '18 at 3:47
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Get generic type of class at runtime
23 answers
I'm working on reflection in java.
I know this is a common question and there are a lot of articles about it but I'm a bit confused and can't seem to find the right solution for what I'm trying to achieve.
I have :
public class ClassA
private List<?> myList;
// getters, setters
"myList" will contain a list of objects (DTOs) :
List<?> myList; // could be List<DTO1> or List<DTO2> ...
I'm trying to get the type of the list from another class like this :
public class ClassB
public void myMethod()
// get type of ClassA.getMyList()
I can only get "ArrayList" when I try using stuff like getClass(), getType()...
Trying to get the class of an element in the list results in "LinkedHashMap".
I really want to achieve this by using a wildcard type for the list.
So is there a way I can get the type of myList with minimal code ?
EDIT :
Thank you all for your answers.
Just one thing : myList
contains elements of type LinkedHashMap
, this wasn't very clear on my post.
It seems this really can't be done. Each LinkedHashMap
represents an object but you can't know which one. They are just a list of keys + values.
java generics reflection wildcard
marked as duplicate by Matthieu Brucher, user6910411, Max Vollmer, KevinO, Zephyr Nov 19 '18 at 3:47
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
did you tryClassA.getMyList().get(0).getClass()
??
– Paplusc
Nov 16 '18 at 11:25
4
You can't. Type erasure.ClassA
should probably be generic.class ClassA<T> private List<T> myList;
– Michael
Nov 16 '18 at 11:27
@Michael but if you do agetClass()
from any element in theList
you will get it, won't you?
– Paplusc
Nov 16 '18 at 11:34
2
@Paplusc What if it's aList<Object>
? The first element could be a string, the second could be an integer, the third could be a HashMap. Getting the class of the first element doesn't tell you much.
– Michael
Nov 16 '18 at 11:35
@Michal True that, i didn't think in that possibility.
– Paplusc
Nov 16 '18 at 11:46
add a comment |
This question already has an answer here:
Get generic type of class at runtime
23 answers
I'm working on reflection in java.
I know this is a common question and there are a lot of articles about it but I'm a bit confused and can't seem to find the right solution for what I'm trying to achieve.
I have :
public class ClassA
private List<?> myList;
// getters, setters
"myList" will contain a list of objects (DTOs) :
List<?> myList; // could be List<DTO1> or List<DTO2> ...
I'm trying to get the type of the list from another class like this :
public class ClassB
public void myMethod()
// get type of ClassA.getMyList()
I can only get "ArrayList" when I try using stuff like getClass(), getType()...
Trying to get the class of an element in the list results in "LinkedHashMap".
I really want to achieve this by using a wildcard type for the list.
So is there a way I can get the type of myList with minimal code ?
EDIT :
Thank you all for your answers.
Just one thing : myList
contains elements of type LinkedHashMap
, this wasn't very clear on my post.
It seems this really can't be done. Each LinkedHashMap
represents an object but you can't know which one. They are just a list of keys + values.
java generics reflection wildcard
This question already has an answer here:
Get generic type of class at runtime
23 answers
I'm working on reflection in java.
I know this is a common question and there are a lot of articles about it but I'm a bit confused and can't seem to find the right solution for what I'm trying to achieve.
I have :
public class ClassA
private List<?> myList;
// getters, setters
"myList" will contain a list of objects (DTOs) :
List<?> myList; // could be List<DTO1> or List<DTO2> ...
I'm trying to get the type of the list from another class like this :
public class ClassB
public void myMethod()
// get type of ClassA.getMyList()
I can only get "ArrayList" when I try using stuff like getClass(), getType()...
Trying to get the class of an element in the list results in "LinkedHashMap".
I really want to achieve this by using a wildcard type for the list.
So is there a way I can get the type of myList with minimal code ?
EDIT :
Thank you all for your answers.
Just one thing : myList
contains elements of type LinkedHashMap
, this wasn't very clear on my post.
It seems this really can't be done. Each LinkedHashMap
represents an object but you can't know which one. They are just a list of keys + values.
This question already has an answer here:
Get generic type of class at runtime
23 answers
java generics reflection wildcard
java generics reflection wildcard
edited Nov 16 '18 at 13:05
Mr.G
asked Nov 16 '18 at 11:24
Mr.GMr.G
33
33
marked as duplicate by Matthieu Brucher, user6910411, Max Vollmer, KevinO, Zephyr Nov 19 '18 at 3:47
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Matthieu Brucher, user6910411, Max Vollmer, KevinO, Zephyr Nov 19 '18 at 3:47
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
did you tryClassA.getMyList().get(0).getClass()
??
– Paplusc
Nov 16 '18 at 11:25
4
You can't. Type erasure.ClassA
should probably be generic.class ClassA<T> private List<T> myList;
– Michael
Nov 16 '18 at 11:27
@Michael but if you do agetClass()
from any element in theList
you will get it, won't you?
– Paplusc
Nov 16 '18 at 11:34
2
@Paplusc What if it's aList<Object>
? The first element could be a string, the second could be an integer, the third could be a HashMap. Getting the class of the first element doesn't tell you much.
– Michael
Nov 16 '18 at 11:35
@Michal True that, i didn't think in that possibility.
– Paplusc
Nov 16 '18 at 11:46
add a comment |
did you tryClassA.getMyList().get(0).getClass()
??
– Paplusc
Nov 16 '18 at 11:25
4
You can't. Type erasure.ClassA
should probably be generic.class ClassA<T> private List<T> myList;
– Michael
Nov 16 '18 at 11:27
@Michael but if you do agetClass()
from any element in theList
you will get it, won't you?
– Paplusc
Nov 16 '18 at 11:34
2
@Paplusc What if it's aList<Object>
? The first element could be a string, the second could be an integer, the third could be a HashMap. Getting the class of the first element doesn't tell you much.
– Michael
Nov 16 '18 at 11:35
@Michal True that, i didn't think in that possibility.
– Paplusc
Nov 16 '18 at 11:46
did you try
ClassA.getMyList().get(0).getClass()
??– Paplusc
Nov 16 '18 at 11:25
did you try
ClassA.getMyList().get(0).getClass()
??– Paplusc
Nov 16 '18 at 11:25
4
4
You can't. Type erasure.
ClassA
should probably be generic. class ClassA<T> private List<T> myList;
– Michael
Nov 16 '18 at 11:27
You can't. Type erasure.
ClassA
should probably be generic. class ClassA<T> private List<T> myList;
– Michael
Nov 16 '18 at 11:27
@Michael but if you do a
getClass()
from any element in the List
you will get it, won't you?– Paplusc
Nov 16 '18 at 11:34
@Michael but if you do a
getClass()
from any element in the List
you will get it, won't you?– Paplusc
Nov 16 '18 at 11:34
2
2
@Paplusc What if it's a
List<Object>
? The first element could be a string, the second could be an integer, the third could be a HashMap. Getting the class of the first element doesn't tell you much.– Michael
Nov 16 '18 at 11:35
@Paplusc What if it's a
List<Object>
? The first element could be a string, the second could be an integer, the third could be a HashMap. Getting the class of the first element doesn't tell you much.– Michael
Nov 16 '18 at 11:35
@Michal True that, i didn't think in that possibility.
– Paplusc
Nov 16 '18 at 11:46
@Michal True that, i didn't think in that possibility.
– Paplusc
Nov 16 '18 at 11:46
add a comment |
2 Answers
2
active
oldest
votes
You can try this. I dont know it will solve your purpose or not but may be help.
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class App
public static void main(String args)
ClassA classA = new ClassA();
List<String> strs = new ArrayList<>();
strs.add("A");
strs.add("B");
classA.setRepos(strs);
String typeOfList = classA.getRepos().getClass().getTypeName();
System.out.println("Type of variable in classA : "+typeOfList);
String typeOfListData = classA.getRepos().get(0).getClass().getTypeName();
System.out.println("Type of data inside the list : "+typeOfListData);
for (String s: (List<String>)classA.getRepos())
System.out.println(s);
ClassA will be like this
import java.util.List;
public class ClassA
private List<?> repos;
public List<?> getRepos()
return repos;
public void setRepos(List<?> repos)
this.repos = repos;
Output will be like this :
Type of variable in classA : java.util.ArrayList
Type of data inside the list : java.lang.String
A
B
add a comment |
Duplicates:
Get generic type of class at runtime
That example demonstrates that any list can hold objects of any classes:
List<String> stringList = new ArrayList<>();
List list = stringList;
list.add(new Object());
for (String s : stringList)
System.out.println(s);
Only thing that you can do - is to test classes of elements if the list contains any.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try this. I dont know it will solve your purpose or not but may be help.
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class App
public static void main(String args)
ClassA classA = new ClassA();
List<String> strs = new ArrayList<>();
strs.add("A");
strs.add("B");
classA.setRepos(strs);
String typeOfList = classA.getRepos().getClass().getTypeName();
System.out.println("Type of variable in classA : "+typeOfList);
String typeOfListData = classA.getRepos().get(0).getClass().getTypeName();
System.out.println("Type of data inside the list : "+typeOfListData);
for (String s: (List<String>)classA.getRepos())
System.out.println(s);
ClassA will be like this
import java.util.List;
public class ClassA
private List<?> repos;
public List<?> getRepos()
return repos;
public void setRepos(List<?> repos)
this.repos = repos;
Output will be like this :
Type of variable in classA : java.util.ArrayList
Type of data inside the list : java.lang.String
A
B
add a comment |
You can try this. I dont know it will solve your purpose or not but may be help.
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class App
public static void main(String args)
ClassA classA = new ClassA();
List<String> strs = new ArrayList<>();
strs.add("A");
strs.add("B");
classA.setRepos(strs);
String typeOfList = classA.getRepos().getClass().getTypeName();
System.out.println("Type of variable in classA : "+typeOfList);
String typeOfListData = classA.getRepos().get(0).getClass().getTypeName();
System.out.println("Type of data inside the list : "+typeOfListData);
for (String s: (List<String>)classA.getRepos())
System.out.println(s);
ClassA will be like this
import java.util.List;
public class ClassA
private List<?> repos;
public List<?> getRepos()
return repos;
public void setRepos(List<?> repos)
this.repos = repos;
Output will be like this :
Type of variable in classA : java.util.ArrayList
Type of data inside the list : java.lang.String
A
B
add a comment |
You can try this. I dont know it will solve your purpose or not but may be help.
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class App
public static void main(String args)
ClassA classA = new ClassA();
List<String> strs = new ArrayList<>();
strs.add("A");
strs.add("B");
classA.setRepos(strs);
String typeOfList = classA.getRepos().getClass().getTypeName();
System.out.println("Type of variable in classA : "+typeOfList);
String typeOfListData = classA.getRepos().get(0).getClass().getTypeName();
System.out.println("Type of data inside the list : "+typeOfListData);
for (String s: (List<String>)classA.getRepos())
System.out.println(s);
ClassA will be like this
import java.util.List;
public class ClassA
private List<?> repos;
public List<?> getRepos()
return repos;
public void setRepos(List<?> repos)
this.repos = repos;
Output will be like this :
Type of variable in classA : java.util.ArrayList
Type of data inside the list : java.lang.String
A
B
You can try this. I dont know it will solve your purpose or not but may be help.
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class App
public static void main(String args)
ClassA classA = new ClassA();
List<String> strs = new ArrayList<>();
strs.add("A");
strs.add("B");
classA.setRepos(strs);
String typeOfList = classA.getRepos().getClass().getTypeName();
System.out.println("Type of variable in classA : "+typeOfList);
String typeOfListData = classA.getRepos().get(0).getClass().getTypeName();
System.out.println("Type of data inside the list : "+typeOfListData);
for (String s: (List<String>)classA.getRepos())
System.out.println(s);
ClassA will be like this
import java.util.List;
public class ClassA
private List<?> repos;
public List<?> getRepos()
return repos;
public void setRepos(List<?> repos)
this.repos = repos;
Output will be like this :
Type of variable in classA : java.util.ArrayList
Type of data inside the list : java.lang.String
A
B
edited Nov 16 '18 at 11:39
answered Nov 16 '18 at 11:34
flopcoderflopcoder
777513
777513
add a comment |
add a comment |
Duplicates:
Get generic type of class at runtime
That example demonstrates that any list can hold objects of any classes:
List<String> stringList = new ArrayList<>();
List list = stringList;
list.add(new Object());
for (String s : stringList)
System.out.println(s);
Only thing that you can do - is to test classes of elements if the list contains any.
add a comment |
Duplicates:
Get generic type of class at runtime
That example demonstrates that any list can hold objects of any classes:
List<String> stringList = new ArrayList<>();
List list = stringList;
list.add(new Object());
for (String s : stringList)
System.out.println(s);
Only thing that you can do - is to test classes of elements if the list contains any.
add a comment |
Duplicates:
Get generic type of class at runtime
That example demonstrates that any list can hold objects of any classes:
List<String> stringList = new ArrayList<>();
List list = stringList;
list.add(new Object());
for (String s : stringList)
System.out.println(s);
Only thing that you can do - is to test classes of elements if the list contains any.
Duplicates:
Get generic type of class at runtime
That example demonstrates that any list can hold objects of any classes:
List<String> stringList = new ArrayList<>();
List list = stringList;
list.add(new Object());
for (String s : stringList)
System.out.println(s);
Only thing that you can do - is to test classes of elements if the list contains any.
answered Nov 16 '18 at 11:47
Aleksandr SemyannikovAleksandr Semyannikov
593217
593217
add a comment |
add a comment |
did you try
ClassA.getMyList().get(0).getClass()
??– Paplusc
Nov 16 '18 at 11:25
4
You can't. Type erasure.
ClassA
should probably be generic.class ClassA<T> private List<T> myList;
– Michael
Nov 16 '18 at 11:27
@Michael but if you do a
getClass()
from any element in theList
you will get it, won't you?– Paplusc
Nov 16 '18 at 11:34
2
@Paplusc What if it's a
List<Object>
? The first element could be a string, the second could be an integer, the third could be a HashMap. Getting the class of the first element doesn't tell you much.– Michael
Nov 16 '18 at 11:35
@Michal True that, i didn't think in that possibility.
– Paplusc
Nov 16 '18 at 11:46