Dhanrajpatil123 / Java-Collection-Framework

0 stars 0 forks source link

4. Vector #5

Open Dhanrajpatil123 opened 3 days ago

Dhanrajpatil123 commented 3 days ago

Vector

Key Features of Vector


Constructor Of Vector


Dhanrajpatil123 commented 3 days ago

Constructor of Vector Practical

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Vector;

public class VariableDataTypeEx {

    public static void main(String[] args) {

        Vector<Integer> vector1 = new Vector<>();
        System.out.println(vector1.capacity());

        Vector<Integer> vector2 = new Vector<>(15);    // custom initial capacity
        System.out.println(vector2.capacity());

        Vector<Integer> vector3 = new Vector<>(5, 3);  // custom capacity increment
        System.out.println(vector3.capacity());
        vector3.add(10);
        vector3.add(10);
        vector3.add(10);
        vector3.add(10);
        vector3.add(10);
        System.out.println(vector3.capacity());
        vector3.add(10);
        System.out.println(vector3.capacity());

        // 1
        Vector<Integer> vector = new Vector<>(Arrays.asList(1, 2, 3, 4, 5));

        // 2
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(1);
        list.add(1);

        Vector<Integer> vector4 = new Vector<>(list);

        System.out.println(vector4);

    }
}
Dhanrajpatil123 commented 3 days ago

Methods in Vector

  1. add ( E e ) : adds an element at the end.
  2. add ( int index, E e ) : insert an element at the specified index.
  3. get ( int index ) : retrieves the element at the specified index.
  4. set ( int index, E e ) : Replace the element at the specified index.
  5. remove ( Object o) : remove the first occurrence of the specified element.
  6. remove ( int index ) : remove the element at specified index.
  7. size ( ) : returns the number of elements in the vector.
  8. isEmpty () : checks if the vector is empty.
  9. contains (Object o ) : check if the vector contains the specified element.
  10. Clear() : removes all elements from the vector.
Dhanrajpatil123 commented 3 days ago
    public static void main(String[] args) {

        Vector<Integer> vector = new Vector<>(5, 3);
        vector.add(10);
        vector.add(20);
        vector.add(30);
        vector.add(40);
        vector.add(50);

        vector.add(1, 200);  //   10, 20, 10, 10, 10, 10

        for (int i = 0; i < vector.size(); i++) {
            System.out.println(vector.get(i));
        }

        System.out.println(vector.get(1));  // 200

        vector.set(2, 30);    // 10, 200, 30, 30, 40, 50

        vector.remove(1);     //  10, 30, 30, 40, 50

        vector.remove(vector.get(3));    // 10, 30, 30, 50

        System.out.println(vector);

        System.out.println(vector.contains(10));   // true

        System.out.println(vector.contains(100));   // false

        System.out.println(vector.isEmpty());   // false

        vector.clear();

        System.out.println(vector.isEmpty());   // true

    }
Dhanrajpatil123 commented 3 days ago

Internal Implementation of Vector


Synchronization and performance

Dhanrajpatil123 commented 3 days ago

Summary

  1. Vector is a legacy synchronized collection class that implements the List interface.
  2. It behaves like a dynamic array and grows as needed.
  3. It provides thread safety but with a performance cost in single-threaded environment.
  4. In modern applications, Arraylist or concurrent alternatives like CopyOnWriteArrayList are typically preffered overr vector unless thread safety is priority.
Dhanrajpatil123 commented 3 days ago

ArrayList Not Thread Safe

public static void main(String[] args) {

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

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                list.add(i);
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                list.add(i);
            }
        });

        t1.start();
        t2.start();

        try{
            t1.join();
            t2.join();
        }
        catch (InterruptedException e){
        }

        System.out.println(list.size());     // size can be any thing as per the elements can store 

    }

If you want the exact size and store all elements then always prefer vector

public static void main(String[] args) {

        Vector<Integer> list = new Vector<>();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                list.add(i);
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                list.add(i);
            }
        });

        t1.start();
        t2.start();

        try{
            t1.join();
            t2.join();
        }
        catch (InterruptedException e){
        }

        System.out.println(list.size());   // size - > 2000

    }