TreeSet和Comparator 对TreeSet排序

使用TreeSet和Comparator,编写TreeSetTestInner类,要求对TreeSet中的元素"HashSet"、"ArrayList"、"TreeMap"、"HashMap"、"TreeSet"、"LinkedList"进行升序和倒序排列,并使用匿名内部类和静态内部类分别实现。

 

方法一:

静态内部类

import java.util.*;

 

public class TreeSetTestInner{

       public static void main(String[] args) {

              TreeSet c = new TreeSet(new MyComparator());  

              c.add("HashSet");

              c.add("ArrayList");

              c.add("TreeMap");

              c.add("HashMap");

              c.add("TreeSet");

              c.add("LinkedList");

   

              Iterator it = c.iterator();

              while(it.hasNext())

                     System.out.println(it.next());

       }

 

       static class MyComparator implements Comparator{

              public int compare(Object o1, Object o2){

                     String s1 = (String)o1;

                     String s2 = (String)o2;

 

                     //升序

                     //return s1.compareTo(s2);

 

                     //降序

                     return -s1.compareTo(s2);

              }

       }

}

 

 

匿名内部类

import java.util.*;

 

public class TreeSetTestInner2{

       public static void main(String[] args) {

              TreeSet c = new TreeSet(new Comparator(){

                     public int compare(Object o1, Object o2){

                            String s1 = (String)o1;

                            String s2 = (String)o2;

 

                            //升序

                            //return s1.compareTo(s2);

 

                            //降序

                            return -s1.compareTo(s2);

                     }

              });  

              c.add("HashSet");

              c.add("ArrayList");

              c.add("TreeMap");

              c.add("HashMap");

              c.add("TreeSet");

              c.add("LinkedList");

   

              Iterator it = c.iterator();

              while(it.hasNext())

                     System.out.println(it.next());

       }    

}

 

方法二:

 

匿名内部类

import java.util.*;

public class TreeSetTest3 {
    
    public static void main(String[] args){
        String[] s = {"HashSet","ArrayList","TreeMap","HashMap","TreeSet","LinkedList"};

        TreeSet sts = new TreeSet();
        TreeSet jts = new TreeSet(new Comparator(){
            public int compare(Object a, Object b){
                String s1 = (String)a;
                String s2 = (String)b;
                if(s1.compareTo(s2) > 0){
                    return -1;
                }
                else if(s1.compareTo(s2) == 0){
                    return 0;
                }
                else{
                    return 1;
                }
            }
        });
    
        for(int i = 0; i < 6; i++){
            jts.add(s[i]);
        }
        for(int i = 0; i < 6; i++){
            sts.add(s[i]);
        }
        System.out.println("升序"+sts);
        System.out.println("降序"+jts);
        
        
    }

}

 

静态内部类:

import java.util.*;

public class TreeSetTest4 {
    static class PaiXu{
        TreeSet sts;
        TreeSet jts;
        public TreeSet daoxu(String[] s){
            jts = new TreeSet(new Comparator(){
            public int compare(Object a, Object b){
                String s1 = (String)a;
                String s2 = (String)b;
                if(s1.compareTo(s2) > 0){
                    return -1;
                }
                else if(s1.compareTo(s2) == 0){
                    return 0;
                }
                else{
                    return 1;
                }
            }
            });
    
            for(int i = 0; i < 6; i++){
                jts.add(s[i]);
            }
            return jts;
        }

        public TreeSet shunxu(String[] s){
            sts = new TreeSet();
            for(int i = 0; i < 6; i++){
                sts.add(s[i]);
            }
            return sts;
        }
    }
    public static void main(String[] args){
        String[] s = {"HashSet","ArrayList","TreeMap","HashMap","TreeSet","LinkedList"};
        PaiXu px = new PaiXu();
        px.shunxu(s);
        px.daoxu(s);
        System.out.println("升序"+px.sts);
        System.out.println("降序"+px.jts);
    }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。