- package Collection;
- import java.util.Comparator;
- import java.util.TreeSet;
- import myutil.SOP;
- public class TestTreeSet {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String[] s = {"abc","abcd","a","b","nn","abc","cdf"};
- function(s);
- }
- static void function(String[] str ){
- TreeSet<String> ts = new TreeSet<String>(new MyComparator());
- for(int i = 0;i < str.length;i++){
- ts.add(str[i]);
- }
- SOP.sop(ts);
- }
-
-
- }
- class MyComparator implements Comparator<String>{
- @Override
- public int compare(String o1, String o2) {
- // TODO Auto-generated method stub
- int len = new Integer(o1.length()).compareTo(new Integer(o2.length()));
- if (0 == len){
- int i = o1.compareTo(o2);
- if (0 == i) {
- return 1;
- }
- return i;
- }
- return len;
- }
-
- }
复制代码 |