Results 1 to 3 of 3
Thread: Qustion about collections
- 01-10-2011, 11:23 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
Qustion about collections
I have an List e.g
userList is basically a collection of User objects and has been casted to Object due to some project need.Java Code:List<Object> userList = new ArrayList<Object>();
My question: Is it possible to convert the entire userlist into List of User objects in one line or I will need to iterate through every object in the list, type cast into User object n add one by one.
- 01-10-2011, 11:48 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Yup, you can do that (with a bit of old fashioned trickery ;-)
The compiler warns about the cast but there's nothing it can do about it because generic objects only exist during compile time (during runtime they're all just Lists). Not that the cast (List<String>) doesn't work and the compiler flags it as an error.Java Code:List<Object> listA= new ArrayList<Object>(); List<String> listB= (List)listA;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-10-2011, 03:36 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 85
- Rep Power
- 0
Similar Threads
-
Help collections
By nikosv in forum New To JavaReplies: 8Last Post: 12-13-2010, 05:29 PM -
Collections
By Cbani in forum New To JavaReplies: 3Last Post: 02-16-2010, 02:46 PM -
Collections Help
By Dr Gonzo in forum New To JavaReplies: 0Last Post: 12-07-2008, 09:15 PM -
Collections Sort
By senthil_jr in forum Advanced JavaReplies: 2Last Post: 06-04-2008, 08:11 AM -
Performance Of Collections
By thomasprabu in forum Advanced JavaReplies: 0Last Post: 01-05-2008, 11:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks