guava学习(一)ComparisonChain 源码分析

本文介绍了如何使用Guava的ComparisonChain进行多条件排序,并深入分析了ComparisonChain的源码,包括start方法、ACTIVE、LESS和GREATER的状态转换,以及InactiveComparisonChain的角色和作用。通过源码解析,揭示了其在比较过程中如何处理相等和不等的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. ComparisonChain 使用
2. ComparisonChain 源码分析

ComparisonChain 使用

我有一个产品的list,想对这个list先按产品生效时间排序,如果生效时间一样再按失效时间排序,代码如下:

package collection;

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

import com.google.common.collect.ComparisonChain;

public class TestListSort {
   

 public static void main(String[] args) {

  List<Product> list = new ArrayList<Product>();

  Product prod1 = new Product(new Date(2015, 4, 21), new Date(2015, 4, 22),
    1);
  list.add(prod1);

  Product prod2 = new Product(new Date(2015, 4, 21), new Date(2015, 4, 21),
    2);
  list.add(prod2);

  Product prod3 = new Product(new Date(2015, 4, 20), new Date(2015, 4, 21),
    3);
  list.add(prod3);
  printList(list);

  sort(list);
  printList(list);

 }

    public static void sort(List<Product> list){
    Collections.sort(list,new Comparator<Product>() {

   @Override
   public int compare(Product p1, Product p2) {
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值