Getting Highest and Lowest Value Element From a Set by Using Sorting Logic on TreeSet in Java
Last Updated :
28 Dec, 2020
TreeSet in java implements the Set Interface and uses a red-black tree for storing values. We use TreeSet for sorting as the elements in TreeSet are stored in ascending order by default. Once the TreeSet is created, we can get the Highest Element Value from the Last element of the TreeSet and the first element value from the first element of TreeSet. We have to pass the Comparator objects along with the TreeSet declaration first. Now, we can simply do this by Creating a TreeSet and adding elements and then sorting the elements by a certain attribute, finally getting the Highest element by last() and lowest by first() functions on TreeSet. We will have to Override the compare method for implementing the Sorting. The TreeSet Declaration would follow the Syntax given below :
Syntax
TreeSet<Comparator> name = new TreeSet<Comparator>(new ComparatorClass());
Parameters
- name: Name of the TreeSet Created
- Comparator: Comparator Object
- ComparatorClass(): Class used for implementing Sorting Function(compare method)
Returns: TreeSet where we can add values
For example, we want to create a TreeSet having names and Age of a group, and we want to find the Person with the highest Age and Value and with the lowest Age and Value
Example 1:
Input :
Name - "Ramesh" Age - 20
Name - "Suresh" Age - 48
Name - "Ankit" Age - 14
Name - "Madhav" Age - 78
Output :
Highest Age Person: Name : Madhav-- Age : 78
Lowest Age Person: Name : Ankit-- Age : 14
Example 2:
Input :
Name - "Ramesh" Age - 20
Name - "Suresh" Age - 20
Name - "Ankit" Age - 78
Name - "Madhav" Age - 78
Output :
Highest Age Person: Name : Ankit-- Age : 78
Lowest Age Person: Name : Suresh-- Age : 14
Explanation
For example1 all the ages are distinct so highest and lowest are found based on age value.
For example2 highest age values are the same so the person name at last() of treeset is given the highest value i.e ‘A’ < ‘M’ so Ankit has the highest age whereas if the lowest age values are the same name at first() of treeset is given lowest value i.e ‘S’ > ‘R’ so Suresh has the lowest age.
Example:
Java
import java.util.*;
import java.io.*;
import java.util.Comparator;
import java.util.TreeSet;
class sorting implements Comparator<ages> {
@Override
public int compare(ages age1, ages age2) {
if (age1.value() > age2.value()) {
return 1 ;
} else {
return - 1 ;
}
}
}
class ages {
private String name;
private int age;
public ages(String name, int a) {
this .name = name;
this .age = a;
}
public String Name() {
return name;
}
public void NewName(String name) {
this .name = name;
}
public int value() {
return age;
}
public void NewAge( int age) {
this .age = age;
}
public String toString() {
return "Name: " + this .name + "-- age: " + this .age;
}
}
public class GFG {
public static void main(String[] args) {
TreeSet<ages> agetree = new TreeSet<ages>( new sorting());
agetree.add( new ages( "Ramesh" , 20 ));
agetree.add( new ages( "Suresh" , 20 ));
agetree.add( new ages( "Ankit" , 78 ));
agetree.add( new ages( "Madhav" , 78 ));
System.out.println( "Highest Age Person: " + agetree.last());
System.out.println( "Lowest Age Person: " + agetree.first());
}
}
|
Output
Highest Age Person: Name: Ankit-- age: 78
Lowest Age Person: Name: Suresh-- age: 20
Share your thoughts in the comments
Please Login to comment...