Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array to List with generics.

 
Tim Resh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, is there any simple way to cast an array to a List. I am using a DAO library that returns an Array of objects that I have to loop and recreate into a List everytime I get data


I would like to use something like


I am looking for a simple way to put the array in a list, collection, etc...

The returning array is in keyed order, in other words the list is in the retreived SQL "Order By" sequence. I do not have access to the DAO source code and I do not have time to rewrite or move to another package that would be more flexible.

Thanks
Mark
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Tim! Dennis's code is exactly right. As a suggestion to you to help you more quickly find answers to your problems, for questions such as this it is probably easier on you to just use Google to find a solution. For example, searching for "Array to ArrayList Java" brings up this exact same code instantly.

We all appreciate questions and they also provide new resources for people to find on Google when searching, but I know from personal experience that it can often times be faster for these smaller questions to just type exactly what you want into Google, even though Dennis was extremely fast in answering!

 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Resh wrote:The returning array is in keyed order, in other words the list is in the retreived SQL "Order By" sequence.

Just as a BTW, if the Users class specifies its own order (or you can specify one for it with a Comparator), you may not even need to worry about whether the array is ordered or not, viz:Of course, it presumes that your ordering can be made unique; otherwise you may end up with fewer items in the Set than you bargained for .
My usual trick is to stick a final comparison for the ID after whatever else you compare on.

Winston
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List to Array?

List<Users> userList = new ArrayList<Users>();

Users [] userArray = (Users[]) userList.toArray();
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:List to Array?

Is that a question or a statement? I believe OP wanted to convert the other way.

Winston
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For converting lists to arrays, we can use the method from the Arrays utility class. However there is no method in Collections class to convert a collection to Array.


I was only trying to show how its done in the reverse way.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:For converting lists to arrays, we can use the method from the Arrays utility class. However there is no method in Collections class to convert a collection to Array.

Actually, there is:and it doesn't need a cast.

Winston
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't I mention "Collections" class?

I was talking about a static method in Collections utility class
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:Didn't I mention "Collections" class?...

You did; my mistake.

However, since Collections generally only contains methods that aren't available elsewhere (even if, arguably, they should have been), it seems logical to me that it wouldn't.

Winston
 
Tim Resh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all. The Arrays.aslist(users) seems to take more memory and time. I believe a copy is being made in memory. I was looking for a way to "wrap" the array with a list. I believe using the new ArrayList<Users>(users) is the most efficient way.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Resh wrote:Thanks to all. The Arrays.aslist(users) seems to take more memory and time. I believe a copy is being made in memory. I was looking for a way to "wrap" the array with a list. I believe using the new ArrayList<Users>(users) is the most efficient way.

Actually, that's precisely what Arrays.asList() runs; and you're quite right: it does copy the array.
You wouldn't want someone mucking about with your array just because you allowed them to run new ArrayList(array) on it would you?

But what you want can be done by wrapping your own class around an array. If you have it extends AbstractList, you only have to implement the get() and size() methods. If you want to be able to update the backing array as well, you'll need to override set(int, E) too.

One point though: If the array is of primitives you'll have to use the relevant wrapper class, and all the boxing and unboxing will likely make it a fair bit slower than an array. But it'll create really fast .

Winston
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Tim! Dennis's code is exactly right. As a suggestion to you to help you more quickly find answers to your problems, for questions such as this it is probably easier on you to just use Google to find a solution. For example, searching for "Array to ArrayList Java" brings up this exact same code instantly.

We all appreciate questions and they also provide new resources for people to find on Google when searching, but I know from personal experience that it can often times be faster for these smaller questions to just type exactly what you want into Google, even though Dennis was extremely fast in answering!





Yes, Arrays.asList(T[] object) is a bad solution for more than one reason

1. it is slow

2. it produces a list backed by an array.

so if try to add an element to the resulting list, you get unsupported operation exception.


so whats the solution?


solution: List<Users> usersList = new ArrayList<Users>();

Users [] userArray = new Users[5];

Collections.addAll(usersList, userArray);

This should be your preferred solution. it is much faster and can use it is a normal list object
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic