java List分组

package javaBean;

import java.util.Date;

/**
 * 客户Bean
 *
 * @author 清茶
 * @create 2017-10-26 10:27
 */
public class CustomerVo {
    private Long id;
    private Long customerId;
    private String customerName;
    private Date date;

    public CustomerVo(Long id, Long customerId, String customerName, Date date) {
        this.id = id;
        this.customerId = customerId;
        this.customerName = customerName;
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

 

 

 1 package service;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * List分组工具
 7  *
 8  * @author 清茶
 9  * @create 2017-10-26 10:28
10  */
11 public class ListSortUtil {
12 
13     public interface GroupBy<T> {
14         T groupBy(Object obj);
15     }
16 
17     public static final <T extends Comparable<T>, D> Map<T, List<D>> group(Collection<D> colls, GroupBy<T> gb) {
18         if (colls == null || colls.isEmpty()) {
19             System.out.println("分组集合不能为空");
20             return null;
21         }
22         if (gb == null) {
23             System.out.println("分组依据不能为空");
24             return null;
25         }
26         Iterator<D> iterator = colls.iterator();
27         Map<T, List<D>> map = new HashMap<T, List<D>>();
28         while (iterator.hasNext()) {
29             D d = iterator.next();
30             T t = gb.groupBy(d);
31             if (map.containsKey(t)) {
32                 map.get(t).add(d);
33             } else {
34                 List<D> list = new ArrayList<D>();
35                 list.add(d);
36                 map.put(t, list);
37             }
38         }
39 
40         return map;
41     }
42 }

 

package test;

import javaBean.CustomerVo;
import service.ListSortUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 测试集合分组算法
 *
 * @author 清茶
 * @create 2017-10-26 10:40
 */
public class TestListSort {

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

        list.add(new CustomerVo(1L, 2000L, "张1", generateDate(2017, 10, 10)));
        list.add(new CustomerVo(2L, 2000L, "张2", generateDate(2017, 10, 10)));
        list.add(new CustomerVo(3L, 2001L, "张3", generateDate(2017, 11, 10)));
        list.add(new CustomerVo(4L, 2001L, "张4", generateDate(2017, 11, 10)));
        list.add(new CustomerVo(5L, 2002L, "张5", generateDate(2017, 12, 10)));
        list.add(new CustomerVo(6L, 2002L, "张6", generateDate(2017, 12, 10)));

        // 按照客户日期进行排序
        Map<Date, List<CustomerVo>> sortMapByDate = sortByDate(list);
        Map<Long, List<CustomerVo>> sortMapByLong = sortByLong(list);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for (Map.Entry<Date, List<CustomerVo>> entry : sortMapByDate.entrySet()) {
            Date key = entry.getKey();
            List<CustomerVo> customerVos = entry.getValue();
            System.out.print("键值为:" + sdf.format(key) + "所包含的客户ID分别为:");
            for (CustomerVo vo : customerVos) {
                System.out.print(vo.getId() + ",");
            }
            System.out.println();
        }
    }

    private static Map<Date, List<CustomerVo>> sortByDate(List<CustomerVo> vos) {
        return ListSortUtil.group(vos, new ListSortUtil.GroupBy<Date>() {
            public Date groupBy(Object obj) {
                CustomerVo vo = (CustomerVo) obj;
                return vo.getDate();
            }
        });
    }

    private static Map<Long, List<CustomerVo>> sortByLong(List<CustomerVo> vos) {
        return ListSortUtil.group(vos, new ListSortUtil.GroupBy<Long>() {
            public Long groupBy(Object obj) {
                CustomerVo vo = (CustomerVo) obj;
                return vo.getId();
            }
        });
    }

    private static Date generateDate(int year, int month, int day) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date returnDate = new Date();
        try {
            System.out.println(sdf.format(date));
            returnDate = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnDate;
    }

}

 

posted @ 2017-10-26 15:04  ゞ清茶℡  阅读(548)  评论(1编辑  收藏  举报