Skip to content

Commit 9e92e44

Browse files
committed
Create TreeSetTest2.java
1 parent b1e751f commit 9e92e44

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Collections/TreeSetTest2.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//version 11.1
2+
3+
//uses Comparator
4+
5+
import java.util.*;
6+
7+
class MarksSort implements Comparator
8+
{
9+
public int compare(Object ob1,Object ob2)
10+
{
11+
Student st1=(Student)ob1;
12+
Student st2=(Student)ob2;
13+
return(st1.marks - st2.marks);
14+
}
15+
}
16+
class RollnoSort implements Comparator
17+
{
18+
public int compare(Object ob1 , Object ob2)
19+
{
20+
Student st1=(Student)ob1;
21+
Student st2=(Student)ob2;
22+
return(st1.rollno - st2.rollno);
23+
}
24+
}
25+
class Student
26+
{
27+
int rollno,marks;
28+
String name;
29+
Student(String name ,int rollno , int marks)
30+
{
31+
this.name=name;
32+
this.rollno=rollno;
33+
this.marks=marks;
34+
}
35+
public String toString()
36+
{
37+
return(name+" "+rollno+" "+marks);
38+
}
39+
}
40+
class TreeSetTest2
41+
{
42+
public static void main(String s[])
43+
{
44+
TreeSet ts=new TreeSet();
45+
ts.add(new Student("abc",103,86));
46+
ts.add(new Student("xyz",101,45));
47+
ts.add(new Student("abc",104,95));
48+
ts.add(new Student("hij",102,75));
49+
50+
Iterator itr=ts.iterator();
51+
while(itr.hasNext())
52+
{
53+
System.out.println(itr.next());
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)