-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComparatorDemo4.java
More file actions
43 lines (34 loc) · 880 Bytes
/
ComparatorDemo4.java
File metadata and controls
43 lines (34 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Program to insert String and StringBuffer Objects into TreeSet where sorting order is increasing length order.
//If two Objects are having same length then consider there alphabetical order.
import java.util.*;
class ComparatorDemo4
{
public static void main(String[] args)
{
TreeSet treeSet = new TreeSet(new MyComparator());
treeSet.add("A");
treeSet.add(new StringBuffer("ABC"));
treeSet.add(new StringBuffer("AA"));
treeSet.add("BB");
treeSet.add("ABCD");
treeSet.add("A");
System.out.println(treeSet);
}
}
class MyComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
String str1 = obj1.toString();
String str2 = obj2.toString();
int len1 = str1.length();
int len2 = str2.length();
if(len1 < len2)
return 1;
else
if(len > len2)
return -1;
else
str1.compareTo(str2);
}
}