精华内容
下载资源
问答
  • Java Comparator

    2014-05-02 23:39:00
    1 Arrays.sort(points, new comparator()); 2 3 public static class comparator implements Comparator<Point> { 4 public int compare(Point p1, Point p2) { 5 return p1.x - p2.x; 6 } ...
    1 Arrays.sort(points, new comparator());
    2 
    3 public static class comparator implements Comparator<Point> {
    4     public int compare(Point p1, Point p2) {
    5     return p1.x - p2.x;
    6 }

     

    转载于:https://www.cnblogs.com/longhorn/p/3704684.html

    展开全文
  • java Comparator

    千次阅读 2012-03-07 10:48:19
    再举个简单例子然后用程序实现。如公司要将员工进行排序(不要说领导排在前面),假设我们的需求比较复杂。...如果对java比较熟悉的会知道java.util.Comparator 接口。要实现里面的函数  int compare(Object o
    再举个简单例子然后用程序实现。如公司要将员工进行排序(不要说领导排在前面),假设我们的需求比较复杂。先进行姓排序,谁的姓拼音靠前,谁就排前面。然后对名字进行排序。恩.如果同名,女性排前头。如果名字和性别都相同,年龄小的排前头。ok,一个也不算复杂的需求。 
    如果对java比较熟悉的会知道java.util.Comparator 接口。要实现里面的函数 
    int compare(Object o1, Object o2) 返回一个基本类型的整型,返回负数表示o1 小于o2,返回0 表示o1和o2相等,返回正数表示o1大于o2。 
    于是我们设计的人员类要有几个变量,firstname,lastname,sex,age分别表示姓,名,性别,年龄。 




    Java代码
    public class Person {       String firstname,lastname;       Boolean sex;       Integer age;       public Person(String firstname,String lastname,Boolean sex,Integer age) {           this.firstname = firstname;           this.lastname = lastname;           this.sex = sex;           this.age = age;       }       public String getFirstName() {            return firstname;           }                public String getLastName() {            return lastname;           }          public Boolean getSex() {             return sex;            }                 public Integer getAge() {             return age;            }             //为了输入方便,重写了toString()      public String toString()            {             return firstname +" "+lastname+" "+(sex.booleanValue()?"男":"女")+" "+age;            }       }       //end person     
    public class Person { String firstname,lastname; Boolean sex; Integer age; public Person(String firstname,String lastname,Boolean sex,Integer age) { this.firstname = firstname; this.lastname = lastname; this.sex = sex; this.age = age; } public String getFirstName() { return firstname; } public String getLastName() { return lastname; } public Boolean getSex() { return sex; } public Integer getAge() { return age; } //为了输入方便,重写了toString() public String toString() { return firstname +" "+lastname+" "+(sex.booleanValue()?"男":"女")+" "+age; } } //end person 


    下面是要实现比较器 


    public class Comparators {    
    public static java.util.Comparator getComparator() {    
        return new java.util.Comparator() {    
       
          public int compare(Object o1, Object o2) {    
            if (o1 instanceof String) {    
              return compare( (String) o1, (String) o2);    
             }else if (o1 instanceof Integer) {    
              return compare( (Integer) o1, (Integer) o2);    
             }else if (o1 instanceof Person) {    
            return compare( (Person) o1, (Person) o2);    
             }else {    
               System.err.println("未找到合适的比较器");    
              return 1;    
       
             }    
           }    
       
          public int compare(String o1, String o2) {    
             String s1 = (String) o1;    
             String s2 = (String) o2;    
            int len1 = s1.length();    
            int len2 = s2.length();    
            int n = Math.min(len1, len2);    
            char v1[] = s1.toCharArray();    
            char v2[] = s2.toCharArray();    
            int pos = 0;    
       
            while (n-- != 0) {    
              char c1 = v1[pos];    
              char c2 = v2[pos];    
              if (c1 != c2) {    
                return c1 - c2;    
               }    
               pos++;    
             }    
            return len1 - len2;    
           }    
       
          public int compare(Integer o1, Integer o2) {    
            int val1 = o1.intValue();    
            int val2 = o2.intValue();    
            return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1));    
       
           }    
          public int compare(Boolean o1, Boolean o2) {    
       
             return (o1.equals(o2)? 0 : (o1.booleanValue()==true?1:-1));    
       
            }    
       
          public int compare(Person o1, Person o2) {    
             String firstname1 = o1.getFirstName();    
             String firstname2 = o2.getFirstName();    
             String lastname1 = o1.getLastName();    
             String lastname2 = o2.getLastName();    
             Boolean sex1 = o1.getSex();    
             Boolean sex2 = o2.getSex();    
             Integer age1 = o1.getAge();    
             Integer age2 = o2.getAge();    
            return (compare(firstname1, firstname2) == 0 ?    
                  (compare(lastname1, lastname2) == 0 ? (compare(sex1, sex2) == 0 ? (compare(age1, age2) == 0 ? 0 :    
                   compare(age1, age2)) :    
                   compare(sex1, sex2)) :    
                   compare(lastname1, lastname2)) :    
                   compare(firstname1, firstname2));    
       
           }    
       
         };    
    }    
       
    }   
    compare(Person o1, Person o2)的返回值看起来比较别扭。最简单的是 


    Java代码
    public int compare(Boolean o1, Boolean o2) {                  return (o1.equals(o2)? 0 : (o1.booleanValue()==true?1:-1));             }     
    public int compare(Boolean o1, Boolean o2) { return (o1.equals(o2)? 0 : (o1.booleanValue()==true?1:-1)); } 


    o1和o2相等返回0,否则o1如果是true 就表示o1大于o2。 
    public class Main {    
    public Main() {    
    }    
    public static void main(String[] args) {    
         Person[] person = new Person[] {    
             new Person("ouyang", "feng", Boolean.TRUE, new Integer(27)),    
             new Person("zhuang", "gw", Boolean.TRUE, new Integer(27)),    
             new Person("zhuang", "gw", Boolean.FALSE, new Integer(27)),    
             new text.Person("zhuang", "gw", Boolean.FALSE, new Integer(2)),    
       
       
          };    
         for (int i = 0; i < person.length; i++) {    
            System.out.println("before sort=" + person[i]);    
          }    
          java.util.Arrays.sort(person, Comparators.getComparator());    
       
         for (int i = 0; i < person.length; i++) {    
             System.out.println("after sort=" + person[i]);    
          }    
       
       
    }    
       
    }   


    java代码 


    before sort=ouyang feng 男 27   
       
    before sort=zhuang gw 男 27   
       
    before sort=zhuang gw 女 27   
       
    before sort=zhuang gw 女 2   
       
    after sort=ouyang feng 男 27   
       
    after sort=zhuang gw 女 2   
       
    after sort=zhuang gw 女 27   
       
    after sort=zhuang gw 男 27   

    展开全文
  • java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子
  • 主要介绍了Java Comparator比较器实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
  • java Comparator Example

    2012-05-31 00:50:41
    http://www.javadeveloper.co.in/java-example/java-comparator-example.html Submitted by Andr on Tue, 10/26/2010 - 20:12 Java Examples package examples; /* Java Comparator example. This Ja.....
    http://www.javadeveloper.co.in/java-example/java-comparator-example.html

    Submitted by Andr on Tue, 10/26/2010 - 20:12
    Java Examples
    package examples;

    /*
    Java Comparator example.
    This Java Comparator example describes how java.util.Comparator
    interface is implemented to compare Java custom class objects.

    This Java Comparator is passed to Collection's sorting method
    (for example Collections.sort method) to perform sorting of Java custom
    class objects.

    */

    import java.util.*;

    /*
    java.util.Comparator interface declares two methods,
    1) public int compare(Object object1, Object object2) and
    2) boolean equals(Object object)
    */

    /*
    We will compare objects of the Employee class using custom comparators
    on the basis of employee age and name.
    */

    class Employee{

    private int age;
    private String name;

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

    public int getAge(){
    return this.age;
    }

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

    public String getName(){
    return this.name;
    }
    }

    /*
    User defined Java comparator.
    To create custom java comparator, implement Comparator interface and
    define compare method.

    The below given comparator compares employees on the basis of their age.
    */

    class AgeComparator implements Comparator{

    public int compare(Object emp1, Object emp2){

    /*
    * parameter are of type Object, so we have to downcast it
    * to Employee objects
    */

    int emp1Age = ((Employee)emp1).getAge();
    int emp2Age = ((Employee)emp2).getAge();

    if(emp1Age > emp2Age)
    return 1;
    else if(emp1Age < emp2Age)
    return -1;
    else
    return 0;
    }

    }

    /*
    The below given comparator compares employees on the basis of their name.
    */

    class NameComparator implements Comparator{

    public int compare(Object emp1, Object emp2){

    //parameter are of type Object, so we have to downcast it to Employee objects

    String emp1Name = ((Employee)emp1).getName();
    String emp2Name = ((Employee)emp2).getName();

    //uses compareTo method of String class to compare names of the employee
    return emp1Name.compareTo(emp2Name);

    }

    }

    /*
    This Java comparator example compares employees on the basis of
    their age and name and sort them in that order.
    */

    public class JavaComparatorExample{

    public static void main(String args[]){

    //Employee array which will hold employees
    Employee employee[] = new Employee[2];

    //set different attributes of the individual employee.
    employee[0] = new Employee();
    employee[0].setAge(40);
    employee[0].setName("Joe");

    employee[1] = new Employee();
    employee[1].setAge(20);
    employee[1].setName("Mark");

    System.out.println("Order of employee before sorting is");
    //print array as is.
    for(int i=0; i < employee.length; i++){
    System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
    }

    /*
    Sort method of the Arrays class sorts the given array.
    Signature of the sort method is,
    static void sort(Object[] object, Comparator comparator)

    IMPORTANT: All methods defined by Arrays class are static. Arrays class
    serves as a utility class.
    */

    //Sorting array on the basis of employee age by passing AgeComparator
    Arrays.sort(employee, new AgeComparator());
    System.out.println("\n\nOrder of employee after sorting by employee age is");

    for(int i=0; i < employee.length; i++){
    System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
    }

    //Sorting array on the basis of employee Name by passing NameComparator
    Arrays.sort(employee, new NameComparator());

    System.out.println("\n\nOrder of employee after sorting by employee name is");
    for(int i=0; i < employee.length; i++){
    System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
    }

    }

    }

    /*
    OUTPUT of the above given Java Comparable Example would be :
    Order of employee before sorting is
    Employee 1 name :: Joe, Age :: 40
    Employee 2 name :: Mark, Age :: 20
    Order of employee after sorting by employee age is
    Employee 1 name :: Mark, Age :: 20
    Employee 2 name :: Joe, Age :: 40
    Order of employee after sorting by employee name is
    Employee 1 name :: Joe, Age :: 40
    Employee 2 name :: Mark, Age :: 20
    */
    展开全文
  • java comparator和comparable自定义比较 想要自定义比较的规则,需要实现comparable接口或者实现comparator接口,可以实现以自己定义的方式去比较,比如根据一个person类的age属性去排序 一 .继承comparable类,重写...

    java comparator和comparable自定义比较

    想要自定义比较的规则,需要实现comparable接口或者实现comparator接口,可以实现以自己定义的方式去比较,比如根据一个person类的age属性去排序

    一 .继承comparable类,重写compare方法
    在这里插入图片描述输出结果
    在这里插入图片描述二 实现comparator接口,重写compareTo方法
    在这里插入图片描述两者都可以实现按照自己的排序规则去进行排序

    展开全文
  • Java Comparator的双重比较问题

    千次阅读 2017-11-28 16:08:12
    Java Comparator的双重比较问题类型推导失败
  • JAVA Comparator实现排序的不同方法 public class ComparatorTest { public static void main(String[] args) { List<People> list = new ArrayList<>(); list.add(new People(1,"张三", ...
  • Java Comparator接口

    万次阅读 2019-05-09 15:33:59
    ------------------------------------------------------...开发工具和关键技术: MyEclipse 10 和 java 撰写时间: 20190503 --------------------------------------------------------------------- Comparator...
  • Java comparator(升序,降序) 官方默认升序,基于: < return -1 = return 0 > return 1 降序,基于 < return 1 = return 0 > return -1 根据其它博客,可看作 comparator反映两个参数的权重,1表示...
  • 浅谈Java Comparator用法

    2015-10-13 10:48:33
    会用Java Comparator会给编程带来很多方便,特别是自定义的对象的排序,至少不用每次都去自己写排序算法,特别是做OJ的时候,时间有限,更加来不及。 1.使用Comparator首先要实现一个自定义的比较器的类,该类...
  • 关于JAVA Comparator 自己的疑惑与解答 疑惑一、为什么一些类的sort方法,只要实现了一个Comparator就可以排序? 解答:一开始钻牛角尖了,排序方法不去实现诸如冒泡排序、快速排序、选择排序之类的具体算法来排序,...
  • Java Comparator 珍藏版

    2019-04-19 08:35:00
    点击蓝色“程序猿DD”关注我哟加个“星标”,不忘签到哦来源:开源技术大全Java java.util.Comparator功能接口对对象集合强制进行排序。要对集合进行排序...
  • Java Comparator 比较器

    2020-03-03 19:05:16
    本文介绍了 JavaComparator 比较器的相关内容。。。
  • JAVA Comparator接口和Comparator接口

    千次阅读 2019-04-18 17:17:36
    java的比较器有两类,分别是Comparable接口和Comparator接口。 在为对象数组进行排序时,比较器的作用非常明显,首先来讲解Comparable接口。 让需要进行排序的对象实现Comparable接口,重写其中的compareTo(T o)方法...
  • java Comparator 实现不一样的排序

    千次阅读 2014-08-18 15:34:13
    java Comparator 实现不一样的排序
  • Java Comparator排序

    千次阅读 2018-04-21 14:24:05
    Collections.sort(mItems, new Comparator() { // 返回值为int类型。 // 大于0表示顺序(递增)【o1 > o2】,小于0表示逆序(递减)【o1 】。 @Override public int compare(Integer o1, Integer o2) { if (o1...
  • java comparator 升序、降序、倒序从源码角度理解
  • JavaComparator的使用

    2020-06-09 13:18:10
    Comparator的使用 步骤: 1、创建Conparator对象 ​ 2、重写compare方法,自定义排序...import java.util.Comparator; public class dff { public static void main(String[] args) { Person p1=new Person("张三
  • java comparator使用

    2018-12-09 19:19:53
    public boolean isAlienSorted(String[] words, String order) { int n = words.length; String[] to = Arrays.copyOf(words, n);... Arrays.sort(to, new Comparator&lt;String&gt;() { ...
  • Java Comparator 排序

    2015-10-01 20:26:38
    当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序。 一、Comparator ...在java.util包中有一个Collections类,里面实现了对
  • java Comparator 实例应用

    2019-01-08 21:59:01
    package cn.hfut.www.SortRefType; import java.util.Comparator; public class GoodsPriceComp implements Comparator&lt;Goods&gt; { @Override public int compare(Goods o1, Goods o2) { ...
  • java Comparator 勘误

    2016-08-31 18:40:32
    之前在java里面 自定义模板类 Pair 又自定义接口Comparator 用于排序Pair类 但之后调用Collections.sort 通过自定义接口对 Pair序列进行排序 一直出现如下错误 Comparison method violates its general ...
  • Java 可以通过 Arrays.sort() 对数组进行排序,默认是升序排列,为了能够对数组进行降序排列,需要传入重写 compare() 方法的 Comparator 接口。 需要注意的是,接口接收的参数只能是引用数据类型,无法接收基本数据...
  • 一、使用方法 重写接口中的compare方法 Comparator<... c=new java.util.Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { return a[1]-b[1]; } }; 二、方法
  • 咨询一个问题,这里定义了一个Comparator 比较器,如果这个函数返回值直接是1,数组元素顺序居然保持不变;如果返回值直接是-1, 数组元素顺序反而是倒序了。 想请大佬答疑解惑,非常感谢。 import java.io....
  • java comparator和comparable

    2016-02-24 10:33:22
    集合中对象的所属类实现了 java.lang.Comparable 接口为集合指定比较器 java.lang.Comparator 的实现类 1. java.lang.Comparable public interface Comparable 接口强行对实现它的每个类的对象进行整体排序。 --...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 11,996
精华内容 4,798
关键字:

javacomparator

java 订阅