接口对象比较大小

/**

  • 要对List进行排序(按照年龄由小到大)

  • collections工具类可比较大小对集合进行排序 实现Comparble实现Comparator

  • collection接口 子接口:list set

  • 1.要实现Comparable接口

  • int compare To(T o) o--要比较的对象

  • 2.要实现Comparator接口 java.util

  • int compare(T o1, T o2) 比较用来排序的两个参数。

  • 比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。

  • @author Administrator

*/
//根据元素的自然顺序(由小到大) 对指定列表按升序排序。
Collections.sort(list);
Iteratoriterator=list.iterator();

    while(iterator.hasNext()) {
        Student student=iterator.next();
        System.out.println(student.getName()+"---"+student.getAge());
    }
    System.out.println("--------------------------------------");
    //由大到小排序
    Comparator comparator=Collections.reverseOrder();
    Collections.sort(list, comparator);
Iteratorit=list.iterator();
    
    while(it.hasNext()) {
        Student student=it.next();
        System.out.println(student.getName()+"---"+student.getAge());
    }
    System.out.println("----------------------------------------");
    //根据姓名来排序
    Collections.sort(list,new  Comparator() {

        @Override
        public int compare(Student o1, Student o2) {
            // TODO Auto-generated method stub
            return o1.getName().compareTo(o2.getName());
        }
        
    });
    Iteratorit1=list.iterator();
    
    while(it1.hasNext()) {
        Student student=it1.next();
        System.out.println(student.getName()+"---"+student.getAge());
    }

你可能感兴趣的:(接口对象比较大小)