views:

5962

answers:

6

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 to List. No other type will do. (I'm developing a test fixture using Slim and FitNesse).


EDIT: Okay, I think I need to give code examples here.

import java.util.*;
public class CustomerCollection
{
    protected LinkedList<Customer> theList;

    public CustomerCollection()
    {
     theList = new LinkedList<Customer>();
    }

    public void addCustomer(Customer c){ theList.add(c); }
    public List<Object> getList()
    {
     return (List<? extends Object>) theList;
    }
}

So in accordance with Yuval A's remarks, I've finally written the code this way. But I get this error:

CustomerCollection.java:31: incompatible types
found   : java.util.List<capture#824 of ? extends java.lang.Object>
required: java.util.List<java.lang.Object>
     return (List<? extends Object>)theList;
            ^
1 error

So, what's the correct way to do this cast?

+6  A: 

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.

Yuval A
You missed my point. Consider: [code] LinkedList<E> ll = someList; List<Object> l =ll [/code]. Is this okay? I mean this.
Here Be Wolves
Did what you said, but problem persists. Edited the question to add code example and the error encountered.
Here Be Wolves
Okay, got the point. Thanks a lot :)
Here Be Wolves
Is it possible for your getList() method to return List<?>?
Andrew Duffy
@Andrew No. Thats the main problem.
Here Be Wolves
A: 

I wonder what you might need that for because LinkedList extends List so there’s no need to cast.

Bombe
check out comment to Yuval A. I've clarified the point.
Here Be Wolves
A: 

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.

Markus Lausberg
I want to convert from LinkedList<String> to List<Object>. That (i used to think) should be automatic, coz LinkedList IS_A List, and String IS_A Object. But I found out today that it is not. :(
Here Be Wolves
A: 

List is an interface, LinkedList is a concrete implementation of that interface. Much of the time an implicit cast will work, assign a LinkedList to a List, or pass it to a function expecting a List and it should just `work'.

An explicit cast can also be done if necessary.

//This is valid
List<Customer> myList = new LinkedList<Customer>();

//Also Valid
List<Customer> myList = (List<Customer>) new LinkedList<Customer>();
Nick
an explicit cast is never necessary.
Yuval A
this isn't necessary
Joset
-1 because this is not the idea of an interface
Markus Lausberg
A: 
>    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> ...

Kris
A: 

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;
}
newacct