The Dynamic Duo of Java Lists: A Deep Dive into ArrayList and Vector

Divine_inner_voice ❤️
5 min readMar 27, 2023

--

List Interface in Java

In Java, the List interface is a member of the Java Collection Framework, which represents an ordered collection of elements that can contain duplicates. The List interface provides several methods to add, remove and access elements in the list. Here are some of the commonly used methods in the List interface:

  1. add(E element) — adds the specified element to the end of the list.
  2. add(int index, E element) — inserts the specified element at the specified position in the list.
  3. get(int index) — returns the element at the specified position in the list.
  4. remove(int index) — removes the element at the specified position in the list.
  5. size() — returns the number of elements in the list.
  6. indexOf(Object o) — returns the index of the first occurrence of the specified element in the list, or -1 if the element is not found.
  7. subList(int fromIndex, int toIndex) — returns a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive.

Note that the List interface is implemented by several classes in the Java API, including ArrayList, LinkedList, and Vector. For a more detailed and brief explanation visit our original article — List Interface in Java

ArrayList in Java

In Java, ArrayList is a class that implements the List interface and provides a resizable-array implementation of the List interface. It is part of the Java Collection Framework and allows for dynamic resizing of the underlying array as elements are added or removed.

Here are some important characteristics of ArrayList:

  1. It maintains the insertion order of elements, so the order in which elements are added to the list is preserved.
  2. It allows duplicate elements.
  3. It provides random access to elements through an index, which means that you can access any element in the list in constant time.
  4. It is not synchronized, which means that it is not thread-safe. If you need to use ArrayList in a multi-threaded environment, you should use the Collections.synchronizedList() method to create a synchronized wrapper around the ArrayList.

Here are some examples of how to use ArrayList

// Create a new ArrayList of Strings
ArrayList<String> list = new ArrayList<>();
// Add elements to the ArrayList
list.add("apple");
list.add("banana");
list.add("cherry");
// Get the size of the ArrayList
int size = list.size();
// returns 3
// Access elements in the ArrayList String
firstElement = list.get(0);
// returns "apple"
// Remove an element from the ArrayList
list.remove(1);
// removes the element at index 1 ("banana")
// Iterate over the elements in the ArrayList using a for-each loop for (String element : list)
{ System.out.println(element); }
// Output:
// apple
// cherry

This code creates an ArrayList of String objects, adds three elements to it (“apple”, “banana”, and “cherry”), gets the size of the list (which is 3), accesses the first element of the list (which is “apple”), removes the second element of the list (“banana”), and then iterates over the remaining elements of the list and prints them to the console. For a more detailed explanation visit our original article on — ArrayList in Java.

Vector Class in Java

In Java, Vector is a class that is similar to the ArrayList class, but it is synchronized, which means it is thread-safe. It also provides methods for resizing the array that is used internally to store its elements. Like ArrayList, Vector implements the List interface and provides a resizable-array implementation of the List interface.

Here are some important characteristics of Vector:

  1. It maintains the insertion order of elements, so the order in which elements are added to the list is preserved.
  2. It allows duplicate elements.
  3. It provides random access to elements through an index, which means that you can access any element in the list in constant time.
  4. It is synchronized, which means that it is thread-safe. This can be useful in multi-threaded environments where multiple threads may be accessing the list concurrently.

Here are some examples of how to use Vector

// Create a new Vector of Strings Vector<String> 
vector = new Vector<>();
// Add elements to the Vector
vector.add("apple");
vector.add("banana");
vector.add("cherry");
// Get the size of the Vector
int size = vector.size();
// returns 3
// Access elements in the Vector String
firstElement = vector.get(0);
// returns "apple"
// Remove an element from the Vector vector.remove(1);
// removes the element at index 1 ("banana")
// Iterate over the elements in the Vector using a for-each loop for (String element : vector)
{ System.out.println(element); }
// Output:
// apple
// cherry

Notice that the usage of Vector is almost identical to the usage of ArrayList. The main difference is that Vector is synchronized, so it is a little slower than ArrayList. However, if you need to use a thread-safe list in a multi-threaded environment, Vector may be a better choice than ArrayList. For a more detailed explanation, you can read our original article on — Vector Class in Java

Conclusion

In conclusion, ArrayList and Vector are two classes in Java that implement the List interface and provide a resizable-array implementation of the List interface. Both classes maintain the insertion order of elements, allow duplicates, and provide random access to elements through an index. The main difference between ArrayList and Vector is that Vector is synchronized, which means that it is thread-safe, while ArrayList is not synchronized.

If you are working in a single-threaded environment, ArrayList is usually the better choice because it is faster than Vector. However, if you are working in a multi-threaded environment where multiple threads may be accessing the list concurrently, Vector may be a better choice because it is thread-safe.

Thank you for reading this article! We hope that you found it informative and useful. Your time and attention are greatly appreciated. If you have any questions or feedback, please feel free to leave a comment below. We are always looking for ways to improve our content and your input is invaluable. Once again, thank you for your support!

References

List Interface in Java

ArrayList in Java.

Vector Class in Java

Originally published at https://www.linkedin.com.

--

--

Divine_inner_voice ❤️

AI-ML| Alchemist |Twin Flames | Intuitive Writer | Spirituality | ✨💖 |Self-realization | Creativity | Art |