Batch - B Practical Test Question

Batch - B Practical Test Question
Batch - B Practical Test Question

Playing with LinkedList using JAVA Programming

import java.lang.*;
import java.util.*;

class Student implements Comparable<Student>
{
int rollno;
String name;

Student(int n, String st)
{
rollno=n;
name=st;
}
public int compareTo(Student s)
{
return this.name.compareTo(s.name);
}
public String toString()
{
return "\n Roll No : "+rollno+"\nName = "+name;
}
public static void main(String s[])
{
LinkedList <Student> l = new LinkedList<Student>();
l.add(new Student(1,"AAA"));
l.add(new Student(2,"BBB"));
l.add(new Student(3,"CCC"));
l.add(new Student(4,"DDD"));

//Creating Clone of LL and Adding Duplicate elements in LL 
LinkedList<Student> l2=(LinkedList <Student>)l.clone();
l2.addAll(l);

//Using SortedMap removing Duplication and Sorting LL
SortedSet<Student> sr = new TreeSet<Student>(l2);

Iterator ir=sr.iterator();
while(ir.hasNext())
{
System.out.println(ir.next());
}

}
}

Comments