Cracking Core Java Interviews 3rd Edition

A Comprehensive Guide to Crack Core Java Interviews in Investment Banks, HealthCare IT & Startups. It covers Core Java, Algorithms, Data Structures, Concurrency, Hibernate and Spring MVC.

Specifically for investment banking domain, healthcare IT and product companies i.e. UBS, RBS, Blackrock, Morgan Stanley, JP Morgan, Nomura, Barclays, Citibank, Markit, Bank of America, Goldman Sachs and other companies i.e. Global Logic, Adobe, hCentive, Edifecs, Expedia, Infosys, TCS, Sapient, Wipro, HCL etc.
Free Chapters PDF
Buy Full PDF ₹250
3rd Edition
Last Updated : Friday, December 4, 2015 3:20:00 PM IST Total Page Hits 335

What is difference between Vector and ArrayList, which one shall be preferred?

One should normally use ArrayList - it offers better performance.

Vector is synchronized for concurrent modification. But Vector synchronizes on each individual operation. That’s almost never what one want to do.

Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time, which would cause a ConcurrentModificationException in the iterating thread) but also slower (why take out a lock repeatedly when once will be enough)?

There is always a overhead (CPU and Memory) of locking even when you don’t need to.

In almost all scenario’s you can utilize ArrayList in your application code, if you are looking for a synchronized version of List you can decorate a collection using the calls such as Collections.synchronizedList()

As for a Stack equivalent - you can have a look at Deque/ArrayDeque.

As a result, Vector is often considered deprecated nowadays.

Main Differences between two Classes

  1. Vector methods is synchronized, ArrayList is not
  2. Data Growth - Vector doubles its size when its full, ArrayList increases its size by 50% when its full.
  3. Vector class retrofitted to implement the List interface, making it a member of the Java Collections Framework in JDK 1.2

Similar Articles

1. Synechron Java Interview Questions

Collection of Java Interview Questions (Core Java, Spring, database and other concepts) for Synechron in banking and finance domain

2. Design Metro Smart Card System for Delhi using Java

Design a program in Java for Metro Smart Card System in Delhi. Evaluation criteria will be based on code completeness, code structure and quality, modularity, usage of OO principles, choice of data structure and unit tests.

3. What does volatile keyword do in a multi-threading environment

volatile keyword helps programmers write thread safe program