ArrayList, LinkedList, Vector

ArrayList, LinkedList, Vector

In Java programming, the ArrayList, LinkedList, and Vector data types are all important elements of a built-in element framework in Java, which helps Java programmers implement the List data type element structure interface. The ArrayList, LinkedList, and Vector class data types in Java are commonly used class objects to store, process, and manipulate group elements of the data type. While the ArrayList, LinkedList, and Vector data types share some similar features and functions, the definitions and declarations vary depending on the individual features, functions, and specific uses of each data type.

ArrayList, LinkedList, Vector

ArrayList Data Type in Java.

In Java programming, the ArrayList data type is a resizable structure in the List data type interface, which can be resized when needed and is a method for properly implementing an ArrayList. ArrayList is a built-in feature of the java.util package in the Java library. It provides Java users with fast, random access to array storage elements based on the stored data element’s value index. Similarly, ArrayList provides Java users with dynamic resizing of array data. This means that adding or removing a new element from an existing ArrayList data type can automatically increase or decrease the size of the list data type.

Features of the ArrayList Data Type.

  • Resizing – When the list data declared in an ArrayList data type reaches its fixed declared capacity, the ArrayList data type automatically resizes itself when needed.
  • Random Access – ArrayList provides Java programs with constant-time access to ArrayList data type elements based on the storage index (O(1) time complexity).
  • Efficient for retrieval – ArrayList is best suited for some particular situations in Java where the Java user needs to access the ArrayList elements multiple times. In this process, the Java user cannot apply multiple insertion or deletion operations to the ArrayList.

Instance of the ArrayList Data Type.

import java.util.ArrayList;

public class ArrayListDataType {

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<>();

// Here we are manually adding the ArrayList elements

list.add(“Java”);

list.add(“Javascript”);

list.add(“Python”);

list.add(“Sql”);

// Here it is, accessing the ArrayList element’s storage location.

System.out.println(“Storage Element index location 3 is – ” + list.get(3)); // Result – SQL

// Here it is, iterating through all ArrayList elements.

for (String language : list) {

System.out.println(language);

}

// Now we remove the element from the array list.

list.remove(“Javascript”);

System.out.println(“After ArrayList element deletion – ” + list);

}

}

LinkedList data type in Java.

In Java programming, the LinkedList data type provides Java users with a list storage element interface, a doubly linked list storage data value element structure. The LinkedList data type is a built-in feature of the java.util package library. It provides improved performance for Java users to insert and remove new elements from LinkedList data type elements. Specifically, when the LinkedList data type operation is performed at the start or middle of the list.

LinkedList data type in Java.

  • Linked Structure – In a Java program, each element in the LinkedList data type is defined as a node, which stores reference addresses to the next and previous elements of the data to access the LinkedList data.
  • Insertions and Deletions – The LinkedList data type is an efficient choice when inserting a new data node at the start or middle of the list (O(1) time complexity) and deleting an existing data node.
  • Slow Random Access – The Java LinkedList data type allows slow access to list data value elements by index (O(n) time complexity) because the user must traverse the entire list from the beginning to the end.
  • Not synchronized – Similar to the ArrayList data type used in Java, the LinkedList data type is not a thread-safe process by default.

Example of the LinkedList data type.

import java.util.LinkedList;

public class LinkedListDataType {

public static void main(String[] args) {

LinkedList<String> list = new LinkedList<>();

// here we manually Adding the LinkedList Data Type node elements

list.add(“Laptop”);

list.add(“Desktop”);

list.add(“Palmtop”);

list.add(“Ultrabook”);

// here we Accessing the LinkedList first elements

System.out.println(“Here is the First element of LinkedList – ” + list.getFirst()); // Result – Laptop

// now we Adding an element at the beginning of LinkedList

list.addFirst(“Macbook”);

// here we Adding an last element at the end of LinkedList

list.addLast(“Apple Imac”);

// Here iterates through the completer LinkedList element node

for (String computer : list) {

System.out.println(computer);

}

// Here we are removing the LinkedList element

list.remove(“Desktop”);

System.out.println(“After LinkedList element removal – ” + list);

}

}

Vector data type in Java.

A Vector data type used in Java programming is structured or defined similarly to the ArrayList data type, with some special differences. The Vector data type provides a dynamic array implementation method, similar to the List data type interface, and is a built-in feature of the java.util library package. Unlike the Vector data type ArrayList, the Vector data type is synchronized in nature, making it thread-safe. As such, this synchronization incurs a performance cost in single-threaded applications.

Features of the Vector data type.

  • Thread-safety – The Vector data type used in Java is synchronized in nature, making it thread-safe when used. However, process synchronization can slow it down in a single-threaded environment.
  • Resizing – The Vector data type can automatically change its size like an ArrayList, but when the Array data type becomes full, it typically doubles in size, resulting in excessive memory usage for its storage space.
  • Outdated – The Vector data type used in Java programs is considered a somewhat outdated data type due to its synchronization overhead, and in most cases, the ArrayList data type is recommended or used, unless thread-safety is defined as a priority.

Example of the Vector data type.

import java.util.Vector;

public class VectorDataType {

public static void main(String[] args) {

Vector<String> vector = new Vector<>();

// here we Adding a manual Vector data type elements

vector.add(“Rust”);

vector.add(“Matlab”);

vector.add(“Ruby”);

vector.add(“Swift”);

//here we Accessing the Vector elements

System.out.println(“Here we access the vector Element at index location 2 – ” + vector.get(2)); // Output: Ruby

// here it Iterating through the Vector list

for (String language : vector) {

System.out.println(language);

}

//here we Removing the vector element

vector.remove(“Matlab”);

System.out.println(“After removal vector element – ” + vector);

}

}

Difference between ArrayList, LinkedList, and Vector.

Each FeatureArrayList data typeLinkedList data typeVector data type
How to ImplementationIt nature is Dynamic array, which increase and decrease  automaticallyIt works like Doubly linked list in booth sideIt supports Dynamic array process mechanism
Use Performance (Random Access/ or notArraylist allow Fast (O(1)) indexing accessLinkedList allow Slow (O(n)) indexing accessVector data type allows Fast (O(1)) indexing access
Operation Performance (data Insertion/or Deletion)In Arraylist data type Slow for beginning/middle (O(n)) insertion and deletion operationIn LinkedList Fast for beginning/middle (O(1)) insertion and deletion processIn vector data type Slow for beginning/middle (O(n)) insertion and deletion process
How to ResizingArraylist data type Doubles in size when it fullLinkedList data type Can grow dynamicallyvector data type Doubles in size when it full
Thread Safety featuresArraylist data type is Not synchronizedLinkedList data type is Not synchronizedvector data type Synchronized (thread-safe)
Where to Use CaseArraylist data type Best for random data access, moderate insertionsLinkedList data type Best for frequent insertions/removalsvector data type Used when thread safety is important
How much Memory UsageArraylist data type Generally uses the less memoryLinkedList data type Uses more memory due to linked structurevector data type Uses more memory due to doubling size on resize

ArrayList, LinkedList, and Vector are all list data type summaries.

  • ArrayList – The ArrayList data type in Java is used when the Java user needs fast access based on index location or needs to perform infrequent data insertions or deletions. Specifically, when the group data size is rarely modified.
  • LinkedList – Use the LinkedList data type in Java when the Java user needs to perform multiple element node insertions or deletions, especially at the start or middle positions of a LinkedList. Avoid this for particular situations that require fast data access based on index.
  • Vector – The Vector data type in Java programs is not used in most modern applications due to synchronization overhead. If you require thread-safety features in an application, use the Vector data type. So you can use other thread-safe structures like ArrayList or CopyOnWriteArrayList in your program.

Leave a Reply