aspose file tools*
The moose likes Java in General and the fly likes Convert LinkedList<Object> to LinkedList<T> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert LinkedList<Object> to LinkedList<T>" Watch "Convert LinkedList<Object> to LinkedList<T>" New topic
Author

Convert LinkedList<Object> to LinkedList<T>

Lance Colton
Ranch Hand

Joined: Nov 25, 2010
Posts: 64

I have an Object Array, which I have converted to a LinkedList of Objects.

I need to convert the Object datatype to another data type (lets say T)


LinkedList<T> newList = ((LinkedList<T>) Arrays.asList( oldList[] ));


This tells me "Cannot convert from List<Object> to LinkedList<T>

anyone know a working way (preferably preference efficient) to convert the data type of the List?


~tak
irc.esper.net - tak, irc.freenode.com - tak11, irc.efnet.org - tak11
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You want to store the elements of an Object[] into a LinkedList<T>. So every element of the array must a T or a child of T. Since there is no way to prove this for the entire array (and I'm assuming that the array not just happens to be an T[]) you'll need to cast and add every element to the LinkedList.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Lance Colton
Ranch Hand

Joined: Nov 25, 2010
Posts: 64

Wouter Oet wrote:You want to store the elements of an Object[] into a LinkedList<T>. So every element of the array must a T or a child of T. Since there is no way to prove this for the entire array (and I'm assuming that the array not just happens to be an T[]) you'll need to cast and add every element to the LinkedList.


Ugh, I was afraid of that. Thank you for the reply
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
Actually it is possible to do what you want. Or it would be, if you didn't have a bigger problem: you're trying to cast to a LinkedList, but you don't have a LinkedList. Arrays.asList() doesn't create a LinkedList, and you have done nothing else that could create a LinkedList here. So there's no way to make that cast succeed.

However if we replace LinkedList with a plain List, that problem goes away, and we can get to what you apparently wanted to ask about. Yes, there is a way:

The key is the double cast on line 4. The cast to raw List causes the compiler to forget the generic parameter <Object> entirely. And the cast to List<String> tells the compiler it has a new generic parameter, <String>. Casting to and from raw types would generate a warning, so using @SuppressWarnings is nice to tell the compiler to shut up about that.

Note that this sort of thing is risky. You are telling the compiler to shut up, because you know what you're doing. That's good, if you actually do know what you're doing. But it can give you confusing results later if it turns out you were wrong.

For fun, try predicting what will happen with the following code. Will it compile? Will it run? Exactly what line will the error (compilation error or runtime exception) occur on? Why?

After trying to decide in advance what the answer is, try it and see. Compile it and run it (if possible), and see what happens. Was it what you expected?
Lance Colton
Ranch Hand

Joined: Nov 25, 2010
Posts: 64

Mike Simmons wrote:
The key is the double cast on line 4. The cast to raw List causes the compiler to forget the generic parameter <Object> entirely. And the cast to List<String> tells the compiler it has a new generic parameter, <String>. Casting to and from raw types would generate a warning, so using @SuppressWarnings is nice to tell the compiler to shut up about that.


That is really cool, I had no idea you could do that thank you

Mike Simmons wrote:
For fun, try predicting what will happen with the following code. Will it compile? Will it run? Exactly what line will the error (compilation error or runtime exception) occur on? Why?

After trying to decide in advance what the answer is, try it and see. Compile it and run it (if possible), and see what happens. Was it what you expected?


Sound's fun, I predict the program will fail at the for loop it will throw a CastException. (will post results)
Lance Colton
Ranch Hand

Joined: Nov 25, 2010
Posts: 64


lscolton@Workstation:~/Desktop$ javac Main.java
lscolton@Workstation:~/Desktop$ java Main
[one, two, three, 4, 5, 6]
one
two
three
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at Main.main(Main.java:14)


So close >.< doesn't fail in the foor loop until it gets to the int's, I thought It would fail before it started iterating.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Why would it? The for-each loop simply takes each elements, casts that to String, and then uses it. It's this cast that fails, but only for the Integers, not for the Strings.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Lance Colton
Ranch Hand

Joined: Nov 25, 2010
Posts: 64

If I thought about it more I probably realized that, however for some reason thought the failure would happen as soon as you attempted to access the list
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert LinkedList<Object> to LinkedList<T>
 
Similar Threads
Generics Doubt
Probleme with generics
Generics
Genrecis and Wildcard
Recursion: returning a value/not returning a value(void)