LINQ的Java等价物是什么?


当前回答

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

...

Edit

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

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

:-)

其他回答

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);

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

...

Edit

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

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

:-)

Lambdas现在以JSR-335的形式在Java8中提供-用于JavaTM编程语言的Lambda表达式

更新:JDK8现已发布,其中包含lambda项目。值得一提的是,目前仍然是MEAP的Java 8 in Action。

阅读Brian Goetz关于lambdas的文章,了解如何在JDK8中实现lambdas,同时了解流、内部迭代、短路和构造函数引用。。还要查看上面的JSR以获得更多示例。

我写了一篇关于在JDK8中使用lambdas的一些优点的博客,名为the Power of the Arrow,NetBeans 8也非常支持将构造转换为JDK8,我也写了一个关于使用NetBeans迁移到JDK8的博客。

对于基本的函数集合,Java8内置了它,大多数主要的非Java JVM语言(Scala、Clojure等)都内置了它。

对于SQL数据库的完全语言集成访问,Scala(在JVM上运行)具有Slick

只需添加另一个备选方案:Java6确实有一个使用javax.persistence.ccriteria包的类型安全数据库查询解决方案。

尽管我必须说,这不是真正的LINQ,因为使用LINQ可以查询任何IEnumerable。