How to Sort Vector Elements using Comparable Interface in Java?
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
import java.util.*;
import java.util.Collections;
import java.util.Vector;
class Student implements Comparable<Student> {
private int id;
public Student( int id)
{
this .id = id;
}
public String toString()
{
return "Student[" + this .id + "]" ;
}
public int getId() { return this .id; }
public int compareTo(Student otherStudent)
{
return this .getId() - otherStudent.getId();
}
}
class GFG {
public static void main(String[] args)
{
Vector<Student> student = new Vector<>();
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 ));
System.out.println(student);
Collections.sort(student);
System.out.println(student);
}
}
|
Output
[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
import java.util.Collections;
import java.util.Vector;
import java.util.*;
class Student implements Comparable<Student> {
String name;
int marks;
int id;
public Student(String name, int marks, int id) {
this .name=name;
this .marks=marks;
this .id=id;
}
public int getMarks(){
return this .marks;
}
public int compareTo(Student otherStudent) {
return this .getMarks()-otherStudent.getMarks();
}
}
class GFG {
public static void main(String[] args) {
Vector<Student> student= new Vector<>();
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 ));
for (Student s : student)
System.out.println( "Name:" +s.name + "->" + "Marks:" +s.marks+ "->" + "ID:" +s.id);
System.out.println() ;
Collections.sort(student);
for (Student s : student)
System.out.println( "Name:" +s.name + "->" + "Marks:" +s.marks+ "->" + "ID:" +s.id);
}
}
|
Output
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
Last Updated :
02 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...