LINQ的Java等价物是什么?


当前回答

没有与LINQ for Java等效的语言。但是有一些外部API看起来像LINQ,比如https://github.com/nicholas22/jpropel-light, https://code.google.com/p/jaque/

其他回答

既然Java8支持lambdas,就可以创建与LINQ非常相似的JavaAPI了。

Jinq是这些新的LINQ风格的Java库之一。

我是这个库的开发人员。它基于五年来使用字节码分析将Java转换为数据库查询的研究。类似于C#的D-LINQ是一个位于实体框架之上的查询层,Jinq能够充当JPA或jOOQ之上的一个查询层。它支持聚合、组和子查询。就连埃里克·梅杰(LINQ的创始人)也承认了金克。

https://code.google.com/p/joquery/

支持不同的可能性,

给定集合,

Collection<Dto> testList = new ArrayList<>();

类型,

class Dto
{
    private int id;
    private String text;

    public int getId()
    {
        return id;
    }

    public int getText()
    {
        return text;
    }
}

滤器

Java 7

Filter<Dto> query = CQ.<Dto>filter(testList)
    .where()
    .property("id").eq().value(1);
Collection<Dto> filtered = query.list();

Java 8

Filter<Dto> query = CQ.<Dto>filter(testList)
    .where()
    .property(Dto::getId)
    .eq().value(1);
Collection<Dto> filtered = query.list();

而且

Filter<Dto> query = CQ.<Dto>filter()
        .from(testList)
        .where()
        .property(Dto::getId).between().value(1).value(2)
        .and()
        .property(Dto::grtText).in().value(new string[]{"a","b"});

排序(也适用于Java 7)

Filter<Dto> query = CQ.<Dto>filter(testList)
        .orderBy()
        .property(Dto::getId)
        .property(Dto::getName)
    Collection<Dto> sorted = query.list();

分组(也适用于Java 7)

GroupQuery<Integer,Dto> query = CQ.<Dto,Dto>query(testList)
        .group()
        .groupBy(Dto::getId)
    Collection<Grouping<Integer,Dto>> grouped = query.list();

联接(也适用于Java 7)

鉴于

class LeftDto
{
    private int id;
    private String text;

    public int getId()
    {
        return id;
    }

    public int getText()
    {
        return text;
    }
}

class RightDto
{
    private int id;
    private int leftId;
    private String text;

    public int getId()
    {
        return id;
    }

    public int getLeftId()
        {
            return leftId;
        }

    public int getText()
    {
        return text;
    }
}

class JoinedDto
{
    private int leftId;
    private int rightId;
    private String text;

    public JoinedDto(int leftId,int rightId,String text)
    {
        this.leftId = leftId;
        this.rightId = rightId;
        this.text = text;
    }

    public int getLeftId()
    {
        return leftId;
    }

    public int getRightId()
        {
            return rightId;
        }

    public int getText()
    {
        return text;
    }
}

Collection<LeftDto> leftList = new ArrayList<>();

Collection<RightDto> rightList = new ArrayList<>();

可以像,

Collection<JoinedDto> results = CQ.<LeftDto, LeftDto>query().from(leftList)
                .<RightDto, JoinedDto>innerJoin(CQ.<RightDto, RightDto>query().from(rightList))
                .on(LeftFyo::getId, RightDto::getLeftId)
                .transformDirect(selection ->  new JoinedDto(selection.getLeft().getText()
                                                     , selection.getLeft().getId()
                                                     , selection.getRight().getId())
                                 )
                .list();

表达

Filter<Dto> query = CQ.<Dto>filter()
    .from(testList)
    .where()
    .exec(s -> s.getId() + 1).eq().value(2);

您可以使用scala,它在语法上类似,实际上可能比linq更强大。

没有什么比LINQ for Java更好的了。

...

Edit

现在,在Java8中,我们介绍了StreamAPI,这在处理集合时是一种类似的事情,但它与Linq不同。

如果您正在寻找的是ORM,比如实体框架,那么您可以尝试Hibernate

:-)

有一个叫做quaere的项目。

这是一个Java框架,它增加了查询集合的能力。

注:根据作者的说法,该项目已不再维护。