• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to Convert one type of Collection object data's to Another Collection Object  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

1.we know that LinkedList is faster than ARRAYLIST on insertion and deletion,For iterating to get data inside object arraylist is faster. Is it possible to use Linked list for insertion after how to convert that object to arraylist for iterating and then to Linked list for deletion? please send me some sample code....

2. How to convert a array details to arraylist and then reverse...
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.

An ArrayList is *not* faster for iteration, only for *random access*.

You cannot really "convert" one type of list to another. What you can do is creating a new list with the elements of the old. Whether that's worth it depends on your usage scenario, of course.

Do you actually have a performance problem?

2.

Take a look at Arrays.asList and Collection.toArray
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only convert (as in "cast") between objects if "instanceof" is true. So, you can convert from ArrayList to List, and from LinkedList to List (since both ArrayList and LinkedList "is-a" List), but you cannot cast from ArrayList to LinkedList.

Now, as to performance differences - it is fair to say that in such cases that you would get a performance improvement on the actual insert/delete and iterate operations, you would lose it when you start converting between List implementations, or going back and forth between Lists and arrays.

But, if you really want to, there is addAll:



The bottom line? Choose the implementation you think will be best for the specific case, and stick with it.
[ November 15, 2006: Message edited by: �dne Brunborg ]
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be a case for creating one collection and then populating another, such as loading a large unordered Set and then sorting it all at once by adding it to a TreeSet. I'd want to see comparisons on real data to prove it's worth while.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Stan James:
There might be a case for creating one collection and then populating another, such as loading a large unordered Set and then sorting it all at once by adding it to a TreeSet. I'd want to see comparisons on real data to prove it's worth while.



If I remember correctly, a TreeSet *cannot* possibly sort them all at once - the whole algorithm is based on element-by-element insertion.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by �dne Brunborg:
You can only convert (as in "cast") between objects if "instanceof" is true. So, you can convert from ArrayList to List, and from LinkedList to List (since both ArrayList and LinkedList "is-a" List), but you cannot cast from ArrayList to LinkedList.



Note that this actually doesn't convert the object, but just the type of the reference.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!