转载:https://my.oschina.net/realfighter/blog/349824

在日常的工作中,我们经常需要对两个对象进行比较,以找出其中的异同, Java中提供了compare/compareTo,我们需要实现一个比较器[Comparator],或者直接实现Comparable接口,不过当 对象的属性很多的时候,我们需要写大量的if else代码,代码不够优雅,Guava为我们简化了这一点,我们可以使用ComparisonChain类来优雅的实现对象之间的比较。

可以用来排序,可作为默认的排序依据。

年纪也不小了,到了相亲的年纪,家里找了两个媒婆:媒婆1和媒婆2,给了钱,媒婆挺办事,都介绍了一个好姑娘,居然还都叫lisa,那么就需要比较一下两个姑娘是不是同一个人了。

我们唯一知道的就是两个姑娘的名字、身高和长相,我们也是通过这三点来进行比较,首先我们构造一个Girl对象,实现Comparable接口,来进行比较,传统方法,我们经常这样做:

class Girl implements Comparable<Girl> {

private String name;//名称
private double height;//身高
private String face;//长相

Girl(String name, double height, String face) {
this.name = name;
this.height = height;
this.face = face;
}

//传统方法我们这样比较
@Override
public int compareTo(Girl girl) {
int c1 = name.compareTo(girl.name);
if (c1 != 0){
System.out.println("两个girl的name不相同");
return c1;
}
int c2 = Double.compare(height, girl.height);
if (c2 != 0){
System.out.println("两个girl的height不相同");
return c2;
}
int c3 = face.compareTo(girl.face);
if(c3 !=0)
System.out.println("两个girl的face不相同");
return c3;
}
}

然后,我们测试一下,居然不是一个人,该选择哪一个好呢?

@Test
public void testCompareTo() {
Girl g1 = new Girl("lisa", 175.00, "nice");
Girl g2 = new Girl("lisa", 175.00, "beauty");
//两个girl的face不相同
System.out.println(g1.compareTo(g2) == 0);//false
}

而使用Guava提供的ComparisonChain类,我们可以这样进行比较,如下:

//使用Guava提供的ComparisonChain我们这样比较
@Override
public int compareTo(Girl girl) {
  return ComparisonChain.start()
    .compare(name, girl.name)
    .compare(height, girl.height)
    .compare(face, girl.face)
    .result();
}

翻开ComparisonChain类的源码,我们发现,它其实是一个抽象类,其提供了主要有三个抽象方法,start()用于返回内部的一个 ComparisonChain实现;重载了许多compare()方法,用于接收各种类型的参数,compare方法返回的仍然是 ComparisonChain对象;result()方法用于返回比较后的结果。

总结:通过对ComparisonChain的学习,以及前面Guava中Obejects实用工具类的学习等,我们发现,Guava中大量存在这种类似的链式编码,熟悉设计模式的不难理解,这正是Builder建造者模式的应用。

guava学习--ComparisonChain的更多相关文章

  1. Guava学习笔记目录

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...

  2. guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁

    guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...

  3. guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...

  4. Guava学习

    Guava学习笔记目录 Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concu ...

  5. [置顶] Guava学习之ArrayListMultimap

    ArrayListMultimap类的继承关系如下图所示: Guava ArrayListMultimap List Multimap 是一个接口,继承自 Multimap 接口.ListMultim ...

  6. [置顶] Guava学习之Splitter

    Splitter:在Guava官方的解释为:Extracts non-overlapping substrings from an input string, typically by recogni ...

  7. [置顶] Guava学习之Iterators

    Iterators类提供了返回Iterator类型的对象或者对Iterator类型对象操作的方法.除了特别的说明,Iterators类中所有的方法都在Iterables类中有相应的基于Iterable ...

  8. [置顶] Guava学习之Lists

    Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...

  9. [置顶] Guava学习之Immutable集合

    Immutable中文意思就是不可变.那为什么需要构建一个不可变的对象?原因有以下几点: 在并发程序中,使用Immutable既保证线程安全性,也大大增强了并发时的效率(跟并发锁方式相比).尤其当一个 ...

随机推荐

  1. NOIP 2014 Day2 T1 无线网络发射器

    #include<iostream> #include<cmath> #include<cstdlib> #include<cstdio> #inclu ...

  2. c# wpf定时器的一种用法

    1.xaml页面 <Window x:Class="EssentialWPF.MainWindow" xmlns="http://schemas.microsoft ...

  3. TensorFlow白皮书

    TensorFlow [1] is an interface for expressing machine learning algorithms, and an implementation for ...

  4. android appwigt

    package com.example.test1; import android.os.Bundle; import android.app.Activity; import android.con ...

  5. 关于 OJ1575的参考题解

    #include <stdio.h>int main(){ int a,b; scanf("%d",&a); b=0; while(a) { b+=a%10; ...

  6. VB默认属性、动态数组、Range对象的默认属性的一点不成熟的想法

    1.默认属性 VB6.0有默认属性的特性.当没有给对象指定具体的属性时,"默认属性"是VB6.0将使用的属性.在某些情形下,省略常用属性名,使代码更为精简. 因为CommandBu ...

  7. LINUX下编译源码时所需提前安装的常用依赖包列表

    yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-deve ...

  8. php : 常用函数

    常用函数: <?php /** * 获取客户端IP * @return [string] [description] */ function getClientIp() { $ip = NULL ...

  9. SVN分支和映射总结和数据库初步使用

    SVN使用总结 常用的诸如检出,update,add就不说了. 1.创建分支-----新建分支目录,在分支文件夹下导入项目folder文件夹.分支不会影响主干trunk的代码,可以随意修改. 2.sv ...

  10. angularjs指令系统系列课程(2):优先级priority,模板template,模板页templateUrl

    今天我们先对 priority,template,templateUrl进行学习 1.priority 可取值:int 作用:优先级 一般priority默认为0,数值越大,优先级越高.当一个dom元 ...