3 min read

Java Basics 9

Java Basics 9
Java Basics 9

What is Vector in java?

In Java, a Vector is a collection class that implements a growable array of objects. Vector implements List Interface.

Like ArrayList it also maintains insertion order but it is rarely used in non-thread environment as it is synchronized and due to which it gives poor performance in searching, adding, delete and update of its elements.Vectors are synchronized, ArrayLists are not.

A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

Three ways to create vector class object:

Vector vec = new Vector();

It creates an empty Vector with the default initial capacity of 10. It means the Vector will be re-sized when the 11th elements needs to be inserted into the Vector.

Note: By default vector doubles its size. i.e. In this case the Vector size would remain 10 till 10 insertions and once we try to insert the 11th element It would become 20 (double of default capacity 10).

Vector object= new Vector(int initialCapacity)
Vector vec = new Vector(3);
Vector object= new vector(int initialcapacity, capacityIncrement)
Vector vec= new Vector(4, 6)

Differences in Array/ArrayList in Java.

In Java, an array is a fixed-size data structure that can hold a homogeneous collection of elements, while an ArrayList is a dynamic and resizable array implementation of the List interface.

Here are some key differences between the two:

  • Size: An array has a fixed size, determined when it is created, while an ArrayList can grow and shrink dynamically as elements are added or removed.
  • Types: An array can only hold a homogeneous collection of elements, while an ArrayList can hold a heterogeneous collection of elements.
  • Primitive Types: An array can hold primitive data types such as int, char, etc, while an ArrayList can only hold objects.
  • Syntax: An array is declared with a specific size and type, for example: int[] myArray = new int[10];. An ArrayList is declared with its type, for example: ArrayList<Integer> myArrayList = new ArrayList<Integer>();
  • Performance: Arrays are generally faster and more memory-efficient than ArrayLists, because they have a smaller overhead and no type-checking.
int[] myArray = new int[10];
ArrayList arrL = new ArrayList();

An array is basic functionality provided by Java. ArrayList is part of the collection framework in Java. Therefore array members are accessed using [], while ArrayList has a set of methods to access elements and modify them. Array is a fixed-size data structure while ArrayList is not. One need not mention the size of the Arraylist while creating its object. Even if we specify some initial capacity, we can add more elements.

Since ArrayList can’t be created for primitive data types, members of ArrayList are always references to objects at different memory locations. Therefore in ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations. In an array, it depends on whether the arrays are of primitive type or object type. In the case of primitive types, actual values are contiguous locations, but in the case of objects, allocation is similar to ArrayList.

In summary, if you need a dynamic and resizable array, and don't mind the overhead of type-checking and a slightly slower performance, use an ArrayList. If you need a fixed-size array and want maximum performance, use an array.


How to sort collections? (there is a direct method)

public void sort(List list): is used to sort the elements of List. List elements must be of Comparable type. Note: String class and Wrapper classes implement the Comparable interface. So if you store the objects of string or wrapper classes, it will be Comparable. Ex. Collections.sort(al);


What is the Difference between Sleep() and Yeild() in java?

In Java, the Thread class has two methods for pausing the execution of a thread: sleep() and yield(). Both methods are used to temporarily pause the execution of a thread, but they have different behaviors and uses.

  • sleep(): The sleep() method causes the currently executing thread to temporarily pause for a specified amount of time. During this time, the thread is in the "sleeping" state and does not consume CPU resources. Once the specified time has elapsed, the thread resumes its execution. The sleep() method can be useful for introducing delays or for creating a specific timing for a task.
  • yield(): The yield() method causes the currently executing thread to temporarily pause and allow other threads of the same priority to execute. The thread is in the "ready" state, but it is not sleeping, consuming CPU resources. Once the other threads of the same priority have had a chance to execute, the original thread may resume its execution. The yield() method is used to improve the performance of multi-threaded applications by allowing the CPU to work on other threads.

In summary, sleep() is used to introduce a delay or a specific timing in a thread, while yield() is used to improve the performance of multi-threaded applications by allowing other threads to execute.