0% found this document useful (0 votes)
18 views

Java 2 Assignment C-05

This document defines a Student class with roll number, name, division, and year attributes. It then defines a Sortbyroll class that implements Comparator to sort students by roll number. Finally, it creates a TreeMap of students sorted by roll number and prints the map, keys, and values.

Uploaded by

Manasvi Dawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java 2 Assignment C-05

This document defines a Student class with roll number, name, division, and year attributes. It then defines a Sortbyroll class that implements Comparator to sort students by roll number. Finally, it creates a TreeMap of students sorted by roll number and prints the map, keys, and values.

Uploaded by

Manasvi Dawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// 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());

You might also like