Chinaunix首页 | 论坛 | 博客
  • 博客访问: 138899
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-15 23:00
文章分类

全部博文(46)

文章存档

2018年(18)

2017年(11)

2015年(14)

2014年(3)

我的朋友

分类: Java

2018-01-14 00:00:56

List里面的对象是订单的节点,比如我们快递的物流状态,这个是需要有序的,所以需要根据订单号进行分组排序。

点击(此处)折叠或打开

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;

  7. public class GroupSortTest {
  8.     public static void main(String[] args) {
  9.     
  10.         List<Order> list = new ArrayList<Order>();
  11.         Order o1 = new Order("10086","3",(long) 1008603);
  12.         Order o2 = new Order("10086","2",(long) 1008602);
  13.         Order o3 = new Order("10086","1",(long) 1008601);
  14.         Order o4 = new Order("10010","1",(long) 1001001);
  15.         Order o5 = new Order("10000","3",(long) 1000003);
  16.         Order o6 = new Order("10010","3",(long) 1001003);
  17.         Order o7 = new Order("10000","1",(long) 1000001);
  18.         Order o8 = new Order("10000","2",(long) 1000002);
  19.         Order o9 = new Order("10010","2",(long) 1001002);
  20.         
  21.         list.add(o1);
  22.         list.add(o2);
  23.         list.add(o3);
  24.         list.add(o4);
  25.         list.add(o5);
  26.         list.add(o6);
  27.         list.add(o7);
  28.         list.add(o8);
  29.         list.add(o9);
  30.         
  31.         Map<String, List<Order>> map = OrderSortByGroup(list);
  32.         
  33.         for(Map.Entry<String, List<Order>> entry : map.entrySet()){
  34.             for(Order o : entry.getValue() ){
  35.                 System.out.println(entry.getKey() +"--"+o.getSeq()+"--"+o.getStatusId());
  36.             }
  37.         }
  38.         
  39.     }
  40.     
  41.     public static Map<String, List<Order>> OrderSortByGroup(List<Order> list){
  42.         Map<String, List<Order>> map = new HashMap<String, List<Order>>();
  43.         for(Order order : list) {
  44.             List<Order> staList = map.get(order.orderNo);
  45.             if(staList==null){
  46.                 staList = new ArrayList<Order>();
  47.             }
  48.             Order od = new Order();
  49.             od.setSeq(order.seq);od.setStatusId(order.statusId);
  50.             staList.add(od);
  51.             
  52.             Collections.sort(staList, new Comparator<Order>() {
  53.                 @Override
  54.                 public int compare(Order o1, Order o2) {
  55.                     return o1.getSeq().compareTo(o2.getSeq());
  56.                 }
  57.             });
  58.             
  59.             map.put(order.orderNo, staList);
  60.         }
  61.         return map;
  62.     }
  63.     
  64. }

  65. class Order {
  66.     String orderNo; //订单号
  67.     String seq; //订单节点序号
  68.     Long statusId; //订单节点主键
  69.     
  70.     public Order() {}
  71.     
  72.     public Order(String orderNo, String seq, Long statusId) {
  73.         super();
  74.         this.orderNo = orderNo;
  75.         this.seq = seq;
  76.         this.statusId = statusId;
  77.     }

  78.     public String getOrderNo() {
  79.         return orderNo;
  80.     }
  81.     public void setOrderNo(String orderNo) {
  82.         this.orderNo = orderNo;
  83.     }
  84.     public String getSeq() {
  85.         return seq;
  86.     }
  87.     public void setSeq(String seq) {
  88.         this.seq = seq;
  89.     }
  90.     public Long getStatusId() {
  91.         return statusId;
  92.     }
  93.     public void setStatusId(Long statusId) {
  94.         this.statusId = statusId;
  95.     }
  96. }
输出结果:

点击(此处)折叠或打开

  1. 10086--1--1008601
  2. 10086--2--1008602
  3. 10086--3--1008603
  4. 10000--1--1000001
  5. 10000--2--1000002
  6. 10000--3--1000003
  7. 10010--1--1001001
  8. 10010--2--1001002
  9. 10010--3--1001003
转载自:https://www.cnblogs.com/jager/p/6072987.html
阅读(1162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~