关闭

List分组

标签: JavaList分组groupBy
159人阅读 评论(0) 收藏 举报
分类:

分组实现

package test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GroupByList<V, T> {

  /**
   * 按照指定字段对list内数据进行分组
   * 
   * @param list
   * @param sortName
   * @return
   * @throws SecurityException
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  public Map<V, List<T>> groupBy(List<T> list, final String groupName)
      throws NoSuchMethodException, SecurityException, IllegalAccessException,
      IllegalArgumentException, InvocationTargetException {
    String method = "get" + groupName.substring(0, 1).toUpperCase() + groupName.substring(1);
    Map<V, List<T>> map = new HashMap<V, List<T>>();
    for (T t : list) {
      Method m1 = t.getClass().getMethod(method, null);
      V groupValue = (V) m1.invoke(t, null);
      if (map.containsKey(groupValue)) {
        List<T> tList = map.get(groupValue);
        tList.add(t);
      }
      else {
        List<T> tList = new ArrayList<T>();
        tList.add(t);
        map.put(groupValue, tList);
      }
    }
    return map;
  }
}

测试代码

package test;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.axis2.databinding.types.soapencoding.Array;

public class GroupByTest {

  public static void main(String[] args) throws NoSuchMethodException, SecurityException,
      IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Employee staff11 = new Employee(21, "1group", 1990, 12, 25, 1200);
    Employee staff12 = new Employee(21, "1group", 1991, 11, 26, 1300);
    Employee staff13 = new Employee(13, "1group", 1992, 13, 27, 1400);

    Employee staff21 = new Employee(21, "2group", 1993, 11, 25, 2200);
    Employee staff22 = new Employee(22, "2group", 1994, 11, 26, 2300);
    Employee staff23 = new Employee(23, "2group", 1995, 12, 27, 2400);

    Employee staff31 = new Employee(21, "3group", 1996, 13, 25, 3200);
    Employee staff32 = new Employee(32, "3group", 1997, 11, 26, 3300);
    Employee staff33 = new Employee(33, "3group", 1998, 12, 27, 3400);
    List<Employee> staffs = new ArrayList<Employee>();
    staffs.add(staff11);
    staffs.add(staff12);
    staffs.add(staff13);
    staffs.add(staff21);
    staffs.add(staff22);
    staffs.add(staff23);
    staffs.add(staff31);
    staffs.add(staff32);
    staffs.add(staff33);
    long start = System.currentTimeMillis();
    GroupByList<Integer, Employee> groupByList = new GroupByList<Integer, Employee>();
    Map<Integer, List<Employee>> map = groupByList.groupBy(staffs, "month");
    for (Map.Entry<Integer, List<Employee>> entry : map.entrySet()) {
      List<Employee> eList = entry.getValue();

      System.out.println("组号为:" + entry.getKey());
      for (Employee employee : eList) {
        System.out.println(employee);
      }

    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }
}

Employee对象

package test;

import java.util.Date;

public class Employee {

  private Integer name;

  private String clazz;

  private Integer year;

  private Integer month;

  private Integer day;

  private double salary;

  public Employee() {
  };

  public Employee(Integer name, String clazz, Integer year, Integer month, Integer day,
      double salary) {
    super();
    this.name = name;
    this.clazz = clazz;
    this.year = year;
    this.month = month;
    this.day = day;
    this.salary = salary;
  }

  /**
   * @return name
   */
  public Integer getName() {
    return name;
  }

  /**
   * @return year
   */
  public Integer getYear() {
    return year;
  }

  /**
   * @return month
   */
  public Integer getMonth() {
    return month;
  }

  /**
   * @return day
   */
  public Integer getDay() {
    return day;
  }

  /**
   * @return salary
   */
  public double getSalary() {
    return salary;
  }

  /**
   * @param name
   *        set name
   */
  public void setName(Integer name) {
    this.name = name;
  }

  /**
   * @param year
   *        set year
   */
  public void setYear(Integer year) {
    this.year = year;
  }

  /**
   * @param month
   *        set month
   */
  public void setMonth(Integer month) {
    this.month = month;
  }

  /**
   * @param day
   *        set day
   */
  public void setDay(Integer day) {
    this.day = day;
  }

  /**
   * @param salary
   *        set salary
   */
  public void setSalary(double salary) {
    this.salary = salary;
  }

  /**
   * @return clazz
   */
  public String getClazz() {
    return clazz;
  }

  /**
   * @param clazz
   *        set clazz
   */
  public void setClazz(String clazz) {
    this.clazz = clazz;
  }

  @Override
  public String toString() {
    return "Employee [name=" + name + ", clazz=" + clazz + ", year=" + year + ", month=" + month
        + ", day=" + day + ", salary=" + salary + "]";
  }

}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4879次
    • 积分:94
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档