How to force the Java method to return a general list of any type?

I would like to write a method that returns java.util.List any type without the need for a cast :

 List<User> users = magicalListGetter(User.class); List<Vehicle> vehicles = magicalListGetter(Vehicle.class); List<String> strings = magicalListGetter(String.class); 

What will the method signature look like? Something like this, maybe (?):

 public List<<?> ?> magicalListGetter(Class<?> clazz) { List<?> list = doMagicalVooDooHere(); return list; } 

Thanks in advance!

+42
java list generics reflection casting
Jul 24 '13 at 17:03
source share
8 answers
 private Object actuallyT; public <T> List<T> magicalListGetter(Class<T> klazz) { List<T> list = new ArrayList<>(); list.add(klazz.cast(actuallyT)); try { list.add(klazz.getConstructor().newInstance()); // If default constructor } ... return list; } 

You can also specify a generic parameter for the method. You correctly determined that you need the correct instance of the class to create things ( klazz.getConstructor().newInstance() ).

+79
Jul 24 '13 at 17:06
source share

No need to even pass a class:

 public <T> List<T> magicalListGetter() { return new ArrayList<T>(); } 
+15
Jul 25 '13 at 4:53 on
source share

We have a List<Object> objectList , which we want to refer to List<T>

 public <T> List<T> list(Class<T> c, List<Object> objectList){ List<T> list = new ArrayList<>(); for (Object o : objectList){ T t = c.cast(o); list.add(t); } return list; } 
+2
Feb 20 '16 at 8:57
source share

You can use the old way:

 public List magicalListGetter() { List list = doMagicalVooDooHere(); return list; } 

or you can use Object and the parent class of everything:

 public List<Object> magicalListGetter() { List<Object> list = doMagicalVooDooHere(); return list; } 

Note. Perhaps there is a better parent class for all the objects you put in the list. For example, Number allows you to place Double and Integer .

+1
Jul 24 '13 at 17:06
source share

Something like that

 publi <T> List<T> magicalListGetter(Class<T> clazz) { List list = doMagicalVooDooHere(); return list; } 
+1
Jul 24 '13 at 17:08
source share

Another option does the following:

 public class UserList extends List<User>{ } public <T> T magicalListGetter(Class<T> clazz) { List<?> list = doMagicalVooDooHere(); return (T)list; } List<User> users = magicalListGetter(UserList.class); 

`

+1
Aug 05 '15 at 8:01
source share

You can simply pass to the list and then check if each item can be sent to T.

 public <T> List<T> asList(final Class<T> clazz) { List<T> values = (List<T>) this.value; values.forEach(clazz::cast); return values; } 
0
Mar 08 '17 at 10:42 on
source share

I am sure that you can completely remove the <stuff> that will generate the warning, and you can use the @ suppress warnings. If you really want it to be generic, but to use any of its elements you will need to do type casting. For example, I created a simple function for sorting bubbles and when sorting a list, I use a common type, which is actually a Comparable array in this case. If you want to use an element, do something like: System.out.println ((Double) arrayOfDoubles [0] + (Double) arrayOfDoubles [1]); because I typed Double (s) into Comparable (s), which is a polymorphism, since all Double (s) inherit from Comparable to simplify sorting through Collections.sort ()

  //INDENT TO DISPLAY CODE ON STACK-OVERFLOW @SuppressWarnings("unchecked") public static void simpleBubbleSort_ascending(@SuppressWarnings("rawtypes") Comparable[] arrayOfDoubles) { //VARS //looping int end = arrayOfDoubles.length - 1;//the last index in our loops int iterationsMax = arrayOfDoubles.length - 1; //swapping @SuppressWarnings("rawtypes") Comparable tempSwap = 0.0;//a temporary double used in the swap process int elementP1 = 1;//element + 1, an index for comparing and swapping //CODE //do up to 'iterationsMax' many iterations for (int iteration = 0; iteration < iterationsMax; iteration++) { //go through each element and compare it to the next element for (int element = 0; element < end; element++) { elementP1 = element + 1; //if the elements need to be swapped, swap them if (arrayOfDoubles[element].compareTo(arrayOfDoubles[elementP1])==1) { //swap tempSwap = arrayOfDoubles[element]; arrayOfDoubles[element] = arrayOfDoubles[elementP1]; arrayOfDoubles[elementP1] = tempSwap; } } } }//END public static void simpleBubbleSort_ascending(double[] arrayOfDoubles) 
-one
Jul 24 '13 at 17:25
source share



All Articles