How to cast generic List types in java?

后端 未结 9 984
离开以前
离开以前 2020-12-09 17:33

Well, I have a class Customer (no base class).

I need to cast from LinkedList to List. Is there any clean way to do this?

Just so you know, I need to cast it

相关标签:
9条回答
  • 2020-12-09 18:10

    You should return a List<?> from your method. Intuitively, getList() returns a list so that the caller can retrieve the items inside. List<?> (which is equivalent to List<? extends Object>) allows that functionality. However, you won't be able to put anything into it via the returned list, because that would not be type safe; but I don't think that is what you need anyway.

    public List<?> getList()
    {
        return theList;
    }
    
    0 讨论(0)
  • 2020-12-09 18:15
    >    public List<Object> getList()
    

    Why are you returning List<Object>? You might as well return List (without generics) since that is equivalent but would make the following code work:

    LinkedList<Customer> theList = new LinkedList<Customer>();
    
    public List getList() {
        return theList;
    }
    

    Casting between Lists with different generic types is tricky and seems unnecessary here.

    Of course you should be returning type List<Customer> ...

    0 讨论(0)
  • 2020-12-09 18:16

    LinkedList implements List, so you can implement

    List< String > list1 = new LinkedList< String >(); 
    

    Or do you want to cast from LinkedList< String > to List< int >? in this case you have to pick every single element and convert it to an integer.

    0 讨论(0)
  • 2020-12-09 18:20

    just put

    public static List<Object> getList() {
        List l = test;
        return l;
    }
    
    0 讨论(0)
  • 2020-12-09 18:25

    You do not need to cast. LinkedList implements List so you have no casting to do here.

    Even when you want to down-cast to a List of Objects you can do it with generics like in the following code:

    LinkedList<E> ll = someList;
    List<? extends Object> l = ll; // perfectly fine, no casting needed
    

    Now, after your edit I understand what you are trying to do, and it is something that is not possible, without creating a new List like so:

    LinkedList<E> ll = someList;
    List<Object> l = new LinkedList<Object>();
    for (E e : ll) {
        l.add((Object) e); // need to cast each object specifically
    }
    

    and I'll explain why this is not possible otherwise. Consider this:

    LinkedList<String> ll = new LinkedList<String>();
    List<Object> l = ll; // ERROR, but suppose this was possible
    l.add((Object) new Integer(5)); // now what? How is an int a String???
    

    For more info, see the Sun Java generics tutorial. Hope this clarifies.

    0 讨论(0)
  • 2020-12-09 18:26

    If your list is of a generic type for eg

    ArrayList<String> strList = new ArrayList<String>(); strList.add("String1"); Object o = strList;

    then Following method should work

    public <T> List<T> getListObjectInBodyOfType(Class<T> classz, Object body) {
        if(body instanceof List<?>){
            List<?> tempList = (List<?>)body;
            if(tempList.size() > 0 && tempList.get(0).getClass().equals(classz)){
                return (List<T>) body;
            }
        }
        return new ArrayList<T>();
    }
    

    How to use it?

    List<String> strList1 = getListObjectInBodyOfType(String.class, o);
    

    as I mentioned before it works if the Object contains generic list, this won't work if you pass a non-generic list with mixed type of elements

    0 讨论(0)
提交回复
热议问题