• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which Collection to choose?

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, I need advise concerning which Collection to use.

I want to be able to do the following with my collection:

1) iterate through collection using next and previous, and if there are no next element go to the first one
2) add items to the end of collection
3) remove items with any index from the collection

At the moment I'm trying to implement this using simple ArrayList with ListIterator. For example, to get the next value from my ArrayList I have written the following:



But probably this way is not a good one? Any comments?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look in the Java™ Tutorials you will find details of all the well-known collections. You are describing a List; ArrayList is a good choice for your requirements. Your method will probably fulfil your requirements for iteration.

Look for the get() add() and remove() methods in the List interface and the ArrayList class.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the requirements that you describe, a LinkedList might be more efficient than an ArrayList.

Especially removing elements from the middle of a collection (requirement 3) is expensive on an ArrayList, but cheap on a LinkedList.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, Jesper. I think I missed that requirement 3.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:Given the requirements that you describe, a LinkedList might be more efficient than an ArrayList.

Especially removing elements from the middle of a collection (requirement 3) is expensive on an ArrayList, but cheap on a LinkedList.


But there's a trade-off. With LinkedList you first need to find the position to remove from before you can remove anything. In the end the two can be just as (in)efficient if you need to remove elements from somewhere in the middle, as both require O(n) time - the LinkedList requires O(n) time for finding the element, then O(1) to remove, whereas for ArrayList it's the other way around.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good to use ArrayList collection object for the requirment.
Because it is easy to use ArrayList and perform the iteration and as well as random access operations .

with LinkedList manipulating is very easy, and as long as its used for iteration purposes only and not for random accessing, it’s the best solution.

ArrayList is slow in manipulating the objects like adding /removing the objects randomly compared to Linked list.
where as Linked list follows the sequential process (no random) it is quite fast in manipulating.

In other words

The difference is in the performance characteristics. Because of the way ArrayList and LinkedList work internally, some operations are more efficient on ArrayList, and some operations are more efficient on LinkedList.

For example, inserting an element in the middle of the list is relatively slow on ArrayList, but fast on LinkedList. And looking up a random element in the list is fast on ArrayList, but slow on LinkedList. Which of the two you should choose depends on what you're going to use the list for...
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"learning Freak", check your private messages. We mean it!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic