// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class Student
int rollno;
String name;
String division;
int year;
public Student(int rollno, String name,String division,int year)
this.rollno = rollno;
this.name = name;
this.division=division;
this.year=year;
public String toString()
return this.rollno + " " + this.name +"" +this.division+ this.year;
class Sortbyroll implements Comparator<Student>
public int compare(Student a, Student b)
return a.rollno - b.rollno;
class Main
{
public static void main(String[] args)
TreeMap<Student, Integer> t2 = new TreeMap<Student, Integer>(new Sortbyroll());
t2.put(new Student(11, "Rita"," A ",2003), 23);
t2.put(new Student(91, "Sam"," C ",2020), 39);
t2.put(new Student(21, "Lucky"," D ",2014), 31);
System.out.println("TreeMap using 2nd constructor is:"+t2);
// Using entrySet()
System.out.println("Key/Value mappings: " + t2.entrySet());
// Using keySet()
System.out.println("Keys: " + t2.keySet());
// Using values()
System.out.println("Values: " + t2.values());