Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector - Collection - ArrayList  RSS feed

 
roul ravashimka
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the difference between a vector, a collection and an arraylist.
I was wondering what the difference is in performance, or the easiest to use with when implementing code.
Greeting,
Roul
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "Vector" is a legacy collection which you probably shouldn't use, unless other legacy code requires you to.
A "collection" is a general term to describe groups of objects, and could include arrays, Vectors, Lists, Sets etc. A java.util.Collection is the interface which all colletion implementations in the JDK use.
An ArrayList is one flavour of avaliable Collection, an implementation of the List interface. It is designed to hold growable sequences of data.
As far as performance goes, each Collection has its pros and cons. For example an ArrayList will give you rapid random access to elements, but will be slower than other Collections for inserts and updates; whereas a LinkedList will be better for insertions and deletions, but slower for random access.
Performance is not the main issue when using most Collections, rather what type of data you are storing: a group of object which are all unique? Use a Set. A group of objects which are sequential? Use a List. A group of objects which are key:value pairs? Use a Map.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!