Vector is a child interface of collection. If we want to represent a group of the individual objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for vector. It is a resizable or growable array. It implements a Serializable, Cloneable, and RandomAccess interface. Every method present in the vector is synchronized and hence vector object is thread-safe.
Comparable interface is used to order the objects of the user-defined class. This interface is present in java.lang’ package and it contains only one method compareTo(object). But it provides only a single sorting sequence i.e we can sort elements on the basis of single data members.
compareTo() method
This method is used to compare the current object with the specified object, and it returns three values depending on the comparison of the two objects. Using Comparable interface we can sort the elements of String class objects, user-define class objects, and all wrapper class objects.
Syntax:
public int compareTo(Object obj);
Return Type: Integer value
- Negative value if and only if the first object has to come before the second object.
- Positive value if and only if the first object has to come after the second object.
- Zero if and only if the first object is equal to the second object.
Collections is a utility class present in java.util‘ package to define several utility methods for collection objects like sorting, searching and reversing, etc.
In order to sort elements of a list, the Collections class defines the two sort methods as follows:
- public static void sort(List l): To sort based on default natural sorting order. In this case, a list should contain a homogeneous object or comparable object otherwise we will get runtime exception also list should not contain null otherwise we will get NullPointerException.
- public static void sort(List l, Comparator c): To sort based on customized sorting orders.
Implementation:
Consider creating a custom class name Students which implements the comparable interface in which we get the id of each student. Inside the driver class, we create a vector of type student class, and then we can sort the vector elements using the comparable interface by comparing the students’ id.
Example 1:
Java
// Java Program to Sort Vector Elements // using Comparable Interface // Importing Collections and Vector classes from // java.util package import java.util.*; import java.util.Collections; import java.util.Vector; // Class 1 // Helper class // Class implementing comparable interface class Student implements Comparable<Student> { // Declaring a variable private int id; // Initializing the above variable through // self constructor of this class public Student( int id) { // 'this' keyword refers to current object itself this .id = id; } // Converting the above Integer variable to String public String toString() { return "Student[" + this .id + "]" ; } // Getting the 'id' public int getId() { return this .id; } // Comparing the ids of two student // using the compareTo() method public int compareTo(Student otherStudent) { return this .getId() - otherStudent.getId(); } } // Class 2 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of Vector type // Declaring object of Student(user-defined type) Vector<Student> student = new Vector<>(); // Adding student id to vector // Custom input elements student.add( new Student( 1 )); student.add( new Student( 2 )); student.add( new Student( 9 )); student.add( new Student( 4 )); student.add( new Student( 34 )); // Print all the elements // before sorting System.out.println(student); // Calling sort() method of collections Collections.sort(student); // Print all the elements // after sorting System.out.println(student); } } |
[Student[1], Student[2], Student[9], Student[4], Student[34]] [Student[1], Student[2], Student[4], Student[9], Student[34]]
Example 2: student name, marks, and id in which we are going to sort the vector on the basis of student marks
Java
// Java Program to Sort Vector Elements // using Comparable Interface // Importing Collection an Vector classes from // java.util package import java.util.Collections; import java.util.Vector; import java.util.*; // Class 1 // Helper class // Class implementing comparable interface class Student implements Comparable<Student> { // // Declaring a variables- student name , marks and id String name; int marks; int id; // Initializing the above variable through // self constructor of this class public Student(String name, int marks, int id) { // 'this' keyword refers to current object itself this .name=name; this .marks=marks; this .id=id; } // Getting the 'id' public int getMarks(){ return this .marks; } // Comparing the ids of two student // using the compareTo() method public int compareTo(Student otherStudent) { return this .getMarks()-otherStudent.getMarks(); } } // Class 2 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of Vector type // Declaring object of Student(user-defined type) Vector<Student> student= new Vector<>(); // Adding student id to vector // Custom input elements student.add( new Student( "Roshan" , 86 , 1 )); student.add( new Student( "Ritik" , 96 , 2 )); student.add( new Student( "Ashish" , 99 , 4 )); student.add( new Student( "Sandeep" , 100 , 9 )); student.add( new Student( "Piyush" , 88 , 34 )); // Iterate over the sorted vector // using for each loop // before calling sort() method for (Student s : student) // Print and display the sorted vector System.out.println( "Name:" +s.name + "->" + "Marks:" +s.marks+ "->" + "ID:" +s.id); // New line System.out.println() ; // Calling sort() method of collections Collections.sort(student); // Iterate over the sorted vector // using for each loop // after calling sort() method for (Student s : student) // Print and display the sorted vector System.out.println( "Name:" +s.name + "->" + "Marks:" +s.marks+ "->" + "ID:" +s.id); } } |
Name:Roshan->Marks:86->ID:1 Name:Ritik->Marks:96->ID:2 Name:Ashish->Marks:99->ID:4 Name:Sandeep->Marks:100->ID:9 Name:Piyush->Marks:88->ID:34 Name:Roshan->Marks:86->ID:1 Name:Piyush->Marks:88->ID:34 Name:Ritik->Marks:96->ID:2 Name:Ashish->Marks:99->ID:4 Name:Sandeep->Marks:100->ID:9