Saturday, July 17, 2021

How to sort elements of vector object on particular column


Using sort() method on vector object we can sort elements of vector object.
For this purpose we need to create our own comparator.
Steps to follow
1. Create own Comparator class by implementing Comparator interface and define compare method.
2. use vector.sort() method and pass the object of your own Comparator implemented class.
3. finally you vector object will have sorted elements as per your compare() method defined.

Please go through the above video for more in details.


 

Sunday, July 4, 2021

Java Collection #20 | Using Vector Part-2

How to add custom object to Vector?
What is the requirement to be able to search an custom object from vector?
Why and how to override equals() method?

Java Collection #19 | Using Vector

Video demonstration 

How to use java.util.Vector
How it is different from java.util.ArrayList
Different ways to add or remove elements.

In Java, a vector is a dynamic array that can grow or shrink in size based on the number of elements it contains. The Vector class in Java is a part of the java.util package and provides methods for manipulating dynamic arrays.

To use the Vector class in Java, we need to import the java.util package at the beginning. Here is an example:



import java.util.Vector;

public class MyClass {
   public static void main(String[] args) {
      // create a vector
      Vector<String> vector = new Vector<String>();
      
      // add elements to the vector
      vector.add("Apple");
      vector.add("Banana");
      vector.add("Orange");
      
      // get the size of the vector
      int size = vector.size();
      System.out.println("Size of vector: " + size);
      
      // access an element in the vector
      String element = vector.get(1);
      System.out.println("Element at index 1: " + element);
      
      // remove an element from the vector
      vector.remove(2);
      
      // print all elements in the vector
      for (String s : vector) {
         System.out.println(s);
      }
   }
}


In this example, we create a vector of strings, add elements to it, get the size of the vector, access an element in the vector, remove an element from the vector, and print all elements in the vector using a for-each loop. Note that the Vector class is synchronized, which means that it is thread-safe and can be used in a multi-threaded environment.



















Friday, July 2, 2021

Requirement to use TreeSet for custom object

 

The video explains about

1. How to use TreeSet?

2. What is the requirement for a custom object to be added to Treeset?

3. What is Comparable and Comparator?

4. When and how to use Comparable?

5. When and how to use Comparator?

6. How to iterate the elements added to the TreeSet object?