aspose file tools
The moose likes Java in General and the fly likes How to Convert one type of Collection object data's to Another Collection Object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to Convert one type of Collection object data Watch "How to Convert one type of Collection object data New topic
Author

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

Bubesh Sankar
Greenhorn

Joined: Nov 12, 2006
Posts: 6
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...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
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 ]

Entia non sunt multiplicanda praeter necessitatem
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jul 11, 2001
Posts: 14112
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to Convert one type of Collection object data's to Another Collection Object
 
Similar Threads
LinkedList
Linked List and Linked Hash Map
How to Convert one type of Collection object data's to Another Collection Object
LinkedList vs ArrayList
ArrayList vs LinkedList?