Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection APIs

 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is difference between Vector and List in java.util package.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List is an interface that is implemented by a number of classes with differeny characteristics (e.g. ArrayList and LinkedList). A more proper comparison would be between Vector and ArrayList. The major difference is that all methods of Vector are synchronized, making it ready to use in multi-threaded code. ArrayList's methods aren't synchronized, making it generally faster. In most cases you should use ArrayList instead of Vector.
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ulf Dittmer.


So, from ArrayList and Vector whatever i use i will get same result barring quickness of ArrayList?

May you make a genral picture of collection APIs for easy understanding as i am feeling lost is the api.


How to decide which class should be synchronized?
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector and Hashtable are the very old datastructure in java.in other words you can these are the two data structure earlier java had. So ,keeping in mind it to use with the mulit-threaded enviornment ,methods of both has been made synchronized( you can exlpore the API). As we know ,synchronization always cause the performance hit.There are sometimes while devloping software where you do not need synchronization there comes the use of ArrayList(depends according to situtation),which gives you linear searching,methods of arraylist are not synchronized.for small size database or elements you can not realise the difference.Both behaves the same way. But genrally it is discourage to use the vector where you do not need synchronization. If you want you can synchronize the methods of Arraylist ,by using the Collections class where no. of utility methods has been given.you can use it according to your need. Hope the above answer will be helful for you anyway.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, from ArrayList and Vector whatever i use i will get same result barring quickness of ArrayList?
Yes, provided that multi-threadedness makes no difference.

May you make a genral picture of collection APIs for easy understanding as i am feeling lost is the api.

You might want to work through the Sun Java Tutorial chapter on Collections.

How to decide which class should be synchronized?

The API designers decided that not all Vector uses needed to be thread-safe, so they gave the developers a choice by providing a class that isn't thread-safe by default. (Of course, using the Collections classes has other benefits as well.) ArrayList can still be used in multi-threaded environments; it's just the developers need to decide which accesses need to be synchronized.
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks you both,


now i feel great thinking of collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic