LINQ的Java等价物是什么?


当前回答

LINQ to Objects-JAVA 8添加了流API,它增加了对值流的函数式操作的支持:

包java.util.stream

Java 8解释:将Lambdas应用于Java集合

LINQ to SQL/Nibernate/等。(数据库查询)-一种选择是使用JINQ,它也使用了新的JAVA 8功能,于2014年2月26日在Github上发布:https://github.com/my2iu/Jinq

Jinq为开发人员提供了一种简单自然的数据库编写方法Java中的查询。您可以像对待普通Java对象一样对待数据库数据存储在集合中。您可以使用正常的Java命令,所有代码将自动转换为优化的数据库查询。最后,LINQ风格查询可用于Java!

JINQ项目现场:http://www.jinq.org/

其他回答

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

Java LINQ to SQL实现。与.NET LINQ相比,提供了完整的语言集成和更大的功能集。

有一个替代的解决方案,库利选择。

Coolection并没有假装是新的lambda,但是我们周围都是旧的遗留Java项目,这个库将对我们有所帮助。它的使用和扩展非常简单,只涵盖集合上最常用的迭代操作,例如:

from(people).where("name", eq("Arthur")).first();
from(people).where("age", lessThan(20)).all();
from(people).where("name", not(contains("Francine"))).all();

我尝试了谷歌的番石榴图书馆。它有一种流利的语言,我认为它接近LINQ。另请参见FunctionalExplained。

List<String> parts = new ArrayList<String>();  // add parts to the collection.    
FluentIterable<Integer> partsStartingA = 
    FluentIterable.from(parts).filter(new Predicate<String>() {
        @Override
        public boolean apply(final String input) {
            return input.startsWith("a");
        }
    }).transform(new Function<String, Integer>() {
        @Override
        public Integer apply(final String input) {
            return input.length();
        }
    });

似乎是一个广泛的Java库。当然没有LINQ简洁,但看起来很有趣。

有一个非常好的图书馆,你可以用来做这个。

位于此处:https://github.com/nicholas22/jpropel-light

Lambdas要到Java8才能使用,所以使用它有点不同,感觉不太自然。