比较和排序的概念,类型参数

/{,- 可以与此对象进行比较的那些对象的类型,此接口强行对实现它的每个类的对象进行整体排序,排序基于的算法基于实体类提供的比较函数,比较和排序的概念,import java.util.List,Student s3 = new Student(),1.java提供的默认list排序方法,而不是根据汉字一般的排序方法

图片 7

Java_集合_ArrayLish Comparator比较排序 小记,comparator排序

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

 

class Teacher_Main {
public static void main(String[] args) {

  ArrayList<Teacher> teacherList = new
ArrayList<Teacher>();
  teacherList.add(new Teacher(“污老湿”, 35));
  teacherList.add(new Teacher(“污老人”, 15));
  teacherList.add(new Teacher(“污小人”, 7));
  teacherList.add(new Teacher(“污大人”, 99));

 

  Collections.sort(teacherList, new Comparator<Teacher>() {

      @Override
  public int compare(Teacher o1, Teacher o2) {

      if (o1.age > o2.age) {
         return -1;}

    else if (o1.age < o2.age) {

         return 1;}

         return 0;}

});

 

   for (int i = 0; i < teacherList.size(); i++) {

*  Teacher teacher = teacherList.get(i);
  System.out.println(teacher);*

———————————————————————————华丽的分水岭

 

 

class Teacher {
  public String name;
  public int age;

  public Teacher(String name, int age) {
  this.name = name;
  this.age = age;
}

@Override
  public String toString() /*转成字符串类型*/{
  return name + “:” + +age;
}

}

 

Comparator相比较排序
小记,comparator排序 import java.util.ArrayList; import
java.util.Collections; import java.util.Comparator; class Teacher_Main
{ p…

comparable

接口 Comparable<T>

项目参数:T – 可以与此对象开始展览相比的那多少个对象的品种

public interface Comparable<T>

此接口强行对贯彻它的每种类的对象开始展览全部排序。这种排序被称为类的理当如此排序,类的
compareTo 方法被可以称作它的理当如此比较艺术

达成此接口的靶子列表(和数组)可以透过 Collections.sort(和
Arrays.sort)进行自动排序。达成此接口的对象足以作为有序映射中的键或有序聚聚集的成分,无需钦定比较器。

负整数、零或正整数,根据此指标是小于、等于照旧抢先内定对象。

compareTo(对象)
if(this. > ) return -1; //高到低排序

事例:学生疏数高到低,年龄低到高排序

图片 1package
com.ij34; /** * Created by Admin on 2018/3/7. */ public class
Student implements Comparable<Student>{ private String name;
private int age; private float score; public String getName() { return
name; } public void setName(String name) { this.name = name; } public
int getAge() { return age; } public void setAge(int age) { this.age =
age; } public float getScore() { return score; } public void
setScore(float score) { this.score = score; } public Student(String
name, int age, float score) { this.name = name; this.age = age;
this.score = score; } @Override public String toString() { return
“Student{” + “name='” + name + ‘\” + “, age=” + age + “, score=” +
score + ‘}’; } @Override public int compareTo(Student o) { if
(this.score > o.score) return -1; else if (this.score < o.score)
return 1; else{ if (this.age > o.age) return 1; else if (this.age
< o.age) return -1; else return 0; } } } View Code
图片 2package
com.ij34; import java.util.ArrayList; import java.util.Arrays; import
java.util.Collections; /** * Created by Admin on 2018/3/7. */ public
class ComparableStudentTest { public static void main(String[] args) {
ArrayList<Student> arrays=new ArrayList<Student>(); Student
a=new Student(“李白”,23,88); Student b=new Student(“张三”,21,80);
Student c=new Student(“李四”,22,78); Student d=new
Student(“萧峰”,24,77); arrays.add(a); arrays.add(b); arrays.add(c);
arrays.add(d); Collections.sort(arrays,Student::compareTo); for(Student
s:arrays){ System.out.println(s); } } } View Code

图片 3

 

 

package com.zuikc.se.collectors;

/**
* 排序和相比器的用法,Comparable与Comparator,java中的排序与相比较
* author:最课程(zuikc.com)
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectorTest {
    public static void main(String[] args) {

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(“a”, 3));
        students.add(new Student(“c”, 2));
        students.add(new Student(“b”, 1));
        students.add(new Student(“d”, 4));
        //Collections.sort(students);
        students.sort(new NameSorter());
        for (Student student : students) {
            System.out.printf(“name:%s, age: %d \n”, student.name,
student.age);
        }
    }
}

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student student) {
        if (age> student.age)
        {
            return 1;
        }
        else if (age == student.age)
        {
            return 0;
        }
        else
        {
            return -1;
        }
        //return
Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
    }
}

class NameSorter implements Comparator<Student>{

    @Override
    public int compare(Student s1, Student s2) {
        return s1.name.compareTo(s2.name);
    }
}

java 如何对 list 对象降序排列
package test.tool.gui.dbtool.util; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Test { public static void main(String[] args) { List list = new ArrayList(); //创造3个学生对象,年龄分别是20、19、21,并将他们相继归入List中 Student s1 = new Student(); s1.setAge; Student s2 = new Student(); s2.setAge; Student s3 = new Student(); s3.setAge; list.add; list.add; list.add; System.out.println(“排序前:”+list); Collections.sort(list, new Comparator(){ / int compare(Student o1, Student o2) 再次来到贰个着力类型的整型, 重返负数表示:o1 小于o2, 返回0 表示:o1和o2相等, 再次回到正数表示:o1大于o2。 / public int compare(Student o1, Student o2) { //根据学生的年龄举办升序排列 if(o1.getAge() > o2.getAge{ return 1; } if(o1.getAge() == o2.getAge{ return 0; } return -1; } }); System.out.println(“排序后:”+list); } } class Student{ private int age; public int getAge() { return age; } public void setAge { this.age = age; } @Override public String toString() { return getAge()+””; } } 怎么样对上面代码按年龄降序排列。

java Collections.sort()完毕List排序的暗中认可方法和自定义方法【转】,collections.sort排序

1.java提供的暗许list排序方法

重要代码:

List<String> list = new ArrayList();list.add(“刘媛媛”);

list.add(“王硕”);
list.add(“李明”);
list.add(“刘迪”);
list.add(“刘布”);

//升序
Collections.sort(list,Collator.getInstance(java.util.Locale.CHINA));//注意:是依附的汉字的拼音的假名排序的,并不是基于汉字相似的排序方法
for(int i=0;i<list.size();i++)
{
    System.out.print(list.get(i));
}
System.out.println(“”);

//降序
Collections.reverse(list);//不钦赐排序法规时,也是依据字母的来排序的
for(int i=0;i<list.size();i++)
{
     System.out.print(list.get(i));
}

出口结果:

李明刘布刘迪刘媛媛孙东海
王辉刘媛媛刘迪刘布李明

 

2.自定义的排序法则:

率先种是model类完毕Comparable接口,重写重写int compareTo(Object o)方法

            model类:

public class StudentDTO implements Comparable
{
 private String name;
 private int age;

public String getName()
{
    return name;
}

public void setName(String name)
{
     this.name = name;
}

public ObjType getType()
{
    return type;
}

public void setAge(int age)
{
     this.age= age;
}

@Override
public int compareTo(Object o)
{

       StudentDTO sdto = (StudentDTO)o;

       int otherAge = sdto.getAge();
      // note: enum-type’s comparation depend on types’ list order of
enum method
      // so, if compared property is enum-type ,then its
comparationfollow ObjEnum.objType order

      return this.age.compareTo(otherAge);
}
}

           主方法:           

public static void main(String[] args)
{
      List<StudentDTO> studentList = new ArrayList();

      StudentDTO s1 = new StudentDTO ();

      s.setName(“yuanyuan”);

      s.setAge(22);

      studentList.add(s1);

                  StudentDTO s1 = new StudentDTO ();

                  s.setName(“lily”);

                  s.setAge(23);

                  studentList.add(s2);

                  Collections.sort(studentList);  //按照age升序 22,23,

                 Collections.reverse(studentList);  //按照age降序 23,22 
 

}

其次种是相比器类达成Comparator接口,重写int compare(Object o1, Object
o2)方法;

           model类:           

public class StudentDTO implements Comparable
{
     private String name;
     private int age;

     public String getName()
     {
         return name;
     }

     public void setName(String name)
    {
         this.name = name;
     }

     public ObjType getType()
     {
         return type;
     }

     public void setAge(int age)
     {
         this.age= age;
     }

           比较器类:

class MyCompartor implements Comparator
{
     @Override
     public int compare(Object o1, Object o2)
    {

           StudentDTO sdto1= (StudentDTO )o1;

           StudentDTO sdto2= (StudentDTO )o2;

           return sdto1.getAge.compareTo(stdo2.getAge())

    }
}

           主方法:

public static void main(String[] args)
{
      List<StudentDTO> studentList = new ArrayList();

      StudentDTO s1 = new StudentDTO ();

      s.setName(“yuanyuan”);

      s.setAge(22);

      studentList.add(s1);

      StudentDTO s1 = new StudentDTO ();

      s.setName(“lily”);

      s.setAge(23);

      studentList.add(s2);

      MyComparetor mc = new MyComparetor();

      Collections.sort(studentList,mc);     //按照age升序 22,23,

      Collections.reverse(studentList,mc);    //按照age降序 23,22   

}

 

 

附注:

1.对于数组的排序方法如下:

String[] names = {“王林”,  “杨宝”, “李镇”, “刘迪”, “刘波”};  
Arrays.sort(names,
com.ibm.icu.text.Collator.getInstance(com.ibm.icu.util.ULocale.SIMPLIFIED_CHINESE));//升序;   
System.out.println(Arrays.toString(names));      

2.对此汉字的排序:能够尝试运用ICU4J会获得更加好的结果,极其是姓为有些生僻字的时候,

用com.ibm.icu.text.Collator替换java.text.Collator,用com.ibm.icu.util.ULocale替换java.util.Locale

3.对于枚举类型的enum1.compareTo(enum2)是根据枚举类型值在概念时的先后顺序比较的,越前面包车型大巴越大,

并非依据值的假名先后顺序比较的。

 

转自:

 

Collections.sort()完结List排序的暗许方法和自定义方法【转】,collections.sort排序
1.java提供的默许list排序方法 主要代码: ListString list = n…

comparator

java.util
接口 Comparator<T>

连串参数:T – 此 Comparator 可以比较的靶子类型

粗犷对某些对象 collection 实行完整排序 的相比函数。能够将 Comparator
传递给 sort 方法(如 Collections.sort
Arrays.sort),进而允许在排序依次上达成标准调整。还足以接纳 Comparator
来决定有些数据结构(如有序 set有序映射)的顺序,或然为那多少个并未有自然顺序的对象
collection 提供排序。

int compare(T o1, T o2)
          比较用来排序的两个参数
boolean equals(Object obj)
          指示某个其他对象是否“等于”此 Comparator

 

图片 4package
com.ij34; /** * Created by Admin on 2018/3/7. */ public class
Student{ private String name; private int age; private float score;
public String getName() { return name; } public void setName(String
name) { this.name = name; } public int getAge() { return age; } public
void setAge(int age) { this.age = age; } public float getScore() {
return score; } public void setScore(float score) { this.score = score;
} public Student(String name, int age, float score) { this.name = name;
this.age = age; this.score = score; } @Override public String toString()
{ return “Student{” + “name='” + name + ‘\” + “, age=” + age + “,
score=” + score + ‘}’; } } View
Code
图片 5package
com.ij34; import java.util.Comparator; /** * Created by Admin on
2018/3/7. */ public class Studentcomparator implements
Comparator<Student>{ public int compare(Student o1,Student o2){
if(o1.getScore()>o2.getScore())return -1; else
if(o1.getScore()<o2.getScore()) return 1; else { if
(o1.getAge()>o2.getAge()) return 1; else if
(o1.getAge()<o2.getAge()) return -1; else return 0; } } } View Code
图片 6package
com.ij34; import java.util.ArrayList; import java.util.Arrays; import
java.util.Collections; /** * Created by Admin on 2018/3/7. */ public
class ComparableStudentTest { public static void main(String[] args) {
ArrayList<Student> arrays=new ArrayList<Student>(); Student
a=new Student(“李白”,23,88); Student b=new Student(“张三”,21,80);
Student c=new Student(“李四”,22,78); Student d=new
Student(“萧峰”,24,77); arrays.add(a); arrays.add(b); arrays.add(c);
arrays.add(d); Collections.sort(arrays,new Studentcomparator());
for(Student s:arrays){ System.out.println(s); } } } View Code

图片 7

 

comparable与comparator用法, comparable
接口 Comparable T 类型参数: T – 能够与此对象开展比较的那一个对象的品种
public interface ComparableT 此接…

这几天主题材料来了,作者不想用年龄实行排序了,小编想用姓名进行排序,该如何是好?要掌握大家只能促成一个排序接口,排序接口唯有一个办法。这年,比较器就派上用场了!

比较器 comparable与comparator用法,

 

 

public class CollectorTest {
    public static void main(String[] args) {

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(“a”, 3));
        students.add(new Student(“c”, 2));
        students.add(new Student(“b”, 1));
        students.add(new Student(“d”, 4));
        //Collections.sort(students);
        students.sort(new NameSorter());
        for (Student student : students) {
            System.out.printf(“name:%s, age: %d \n”, student.name,
student.age);
        }
    }
}

class Student implements Comparable{
    public String name;
    public int age;
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Object o) {
        Student student = (Student)o;
        if (age> student.age)
        {
            return 1;
        }
        else if (age == student.age)
        {
            return 0;
        }
        else
        {
            return -1;
        }
        //return
Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
    }
}

class NameSorter implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Student s1 =(Student) o1;
        Student s2 =(Student) o2;
        return s1.name.compareTo(s2.name);
    }
}

public class CollectorTest {
    public static void main(String[] args) {

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(“a”, 3));
        students.add(new Student(“c”, 2));
        students.add(new Student(“b”, 1));
        students.add(new Student(“d”, 4));
        for (Student student : students) {
            System.out.printf(“name:%s, age: %d \n”, student.name,
student.age);
        }
    }
}

class Student {
    public String name;
    public int age;
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }
}

 

2:Comparable与Comparator