HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Collections Framework / Use Array instead of Vector.elementAt() for Better Performance

Use Array instead of Vector.elementAt() for Better Performance

Vector is a another legacy implementation of List interface provided with java bundle. It is almost similar to ArrayList expect it is synchronized also. It has its own advantages as well as disadvantages e.g. Vector doesn’t need additional synchronization while accessed from multiple threads but it degrades its performance for the same reason.

Here I am continuing to analyze the  performance of various java code constructs/ statements. It helps in determining the best practices in your next coding assignment.

In this post, I will compare two ways to access the elements from a Vector and see which one is more effective.

Method 1) Using method elementAt()

In this way, we will use the normal practice for accessing elements i.e. using elementsAt().

int size = v.size();
for(int i=size; i>0; i--)
{
    String str = v.elementAt(size-i);    
}

Method 2) Using array from toArray()

int size = v.size();
String vArr[] = (String[]) v.toArray();
for(int i=0; i

Comparing both methods

In below code, I am comparing both above methods for a single instance of vector class. I will iterate over all elements of vector in loop with both ways. Then we will capture the time taken in both ways of accessing the elements.

package com.howtodoinjava.demo.core.howtodoinjava;

import java.util.Calendar;
import java.util.Vector;

@SuppressWarnings("unused")
public class VectorPerformance
{
	public static void method1(Vector v)
	{
		int size = v.size();
		for(int i=size; i>0; i--)
		{
			String str = v.elementAt(size-i);
		}
	}

	public static void method2(Vector v)
	{
		int size = v.size();
		String[] vArr = new String[size];
		v.toArray(vArr);
		for(int i=0; i<size ; i++)
		{
			String str = vArr[i];
		}
	}

	public static void main(String[] args) {
		Vector<String> vector = new Vector<String>();
		for(int i=0;i<1000000;i++)
		{
			vector.add(""+i);
		}

		long startTime = Calendar.getInstance().getTimeInMillis();
		method1(vector);
		long endTime = Calendar.getInstance().getTimeInMillis();
		System.out.println("Using elementAt() :: " + (endTime - startTime) + " ms");

		startTime = Calendar.getInstance().getTimeInMillis();
		method2(vector);
		endTime = Calendar.getInstance().getTimeInMillis();
		System.out.println("Using array :: " + (endTime - startTime) + " ms");
	}
}

Output:

Using elementAt() :: 30 ms
 Using array :: 6 ms

Conclusion

As you can see that both are pretty fast for normal size collections but comparing against each other array method is way ahead in performance by 5 times. If high usage even this could be considerable. Isn’t it?

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Vijaykumar

    November 19, 2013

    Thanks for your explanation. I want to point out something in your code. Please correct me if I am wrong. It is better to System.currentTimeMillis() rather than Calendar.getInstance().getTimeInMillis(). Calendar object is considered to be the heavy object. Though it is out of context but just wanted point it out. Thank you.

Comments are closed on this article!

Search Tutorials

Java Collections

  • Collections Framework
  • Array
  • ArrayList
  • LinkedList
  • HashMap
  • Hashtable
  • LinkedHashMap
  • TreeMap
  • HashSet
  • LinkedHashSet
  • TreeSet
  • Comparable
  • Comparator
  • Iterator
  • ListIterator
  • Spliterator
  • PriorityQueue
  • PriorityBlockingQueue
  • ArrayBlockingQueue
  • LinkedTransferQueue
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet
  • Collection Sorting
  • Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)