Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
在Java中,我正在寻找一个基于类的某些属性执行POJO分组的框架。

例如:简单的例子

Salary {
int userId;
Double salary;
String office;
}


汇总1:在办公室获得平均薪水。

汇总2:所有薪金总和

汇总3:按办公室划分的薪金总和

最佳答案

您可以使用http://code.google.com/p/lambdaj/

关于兰伯达(Lambdaj):

lambdaj旨在轻松操纵集合。其功能旨在过滤,转换,索引和聚合集合的项目,而无需对其进行显式迭代。

举个例子;

Salary.java

package x;

public class Salary {
    int userId;
    Double salary;
    String office;
    public Salary(int userId, Double salary, String office) {
        super();
        this.userId = userId;
        this.salary = salary;
        this.office = office;
    }
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public Double getSalary() {
        return salary;
    }
    public void setSalary(Double salary) {
        this.salary = salary;
    }
    public String getOffice() {
        return office;
    }
    public void setOffice(String office) {
        this.office = office;
    }


}


LambdaX.java

package x;

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matchers;

import static ch.lambdaj.Lambda.*;

public class LambdajX {

    public static void main(String[] args) {
        List<Salary> salaries = new ArrayList<Salary>();
        salaries.add(new Salary(1, 100.0, "mexico"));
        salaries.add(new Salary(2, 130.0, "germany"));
        salaries.add(new Salary(3, 190.0, "mexico"));

        LambdajX lambdajX = new LambdajX();

        System.out.println("Total Salary : "
                + lambdajX.getAggregateSalary(salaries));
        System.out.println("Total Salary in Mexico : "
                + lambdajX.getAggregateSalaryByOffice(salaries, "mexico"));

        System.out.println("Avg Salary : "
                + lambdajX.getAverageSalary(salaries));

        System.out.println("Avg Salary in Mexico :"
                + lambdajX.getAverageSalaryByOffice(salaries, "mexico"));

    }

    public Double getAggregateSalary(List<Salary> salaries) {
        return sum(salaries, on(Salary.class).getSalary());
    }

    public Double getAggregateSalaryByOffice(List<Salary> salaries,
            String office) {

        return sumFrom(
                select(salaries,
                        having(on(Salary.class).getOffice(),
                                Matchers.equalTo(office)))).getSalary();

    }

    public Double getAverageSalary(List<Salary> salaries) {
        return avg(salaries, on(Salary.class).getSalary());
    }

    public Double getAverageSalaryByOffice(List<Salary> salaries, String office) {

        return avgFrom(
                select(salaries,
                        having(on(Salary.class).getOffice(),
                                Matchers.equalTo("mexico")))).getSalary();

    }
}


使用的库:lambdaj-2.4-with-dependencies.jar

您可以从http://lambdaj.googlecode.com/files/lambdaj-2.4-with-dependencies.jar下载此jar文件

另一种选择是谷歌的番石榴。

10-07 15:56