LINQ的Java等价物是什么?


当前回答

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相比,提供了完整的语言集成和更大的功能集。

您可以使用lambdaj库以更易读的方式选择集合中的项目(以及更多)

https://code.google.com/archive/p/lambdaj/

它比Quaere库有一些优势,因为它不使用任何魔法字符串,它完全是类型安全的,在我看来,它提供了一个更可读的DSL。

你可以试试我的图书馆CollectionsQuery。它允许在对象集合上运行类似LINQ的查询。您必须传递谓词,就像在LINQ中一样。如果您使用的是java6/7,则必须对接口使用旧语法:

List<String> names = Queryable.from(people)
                                    .filter(new Predicate<Person>() {
                                                public boolean filter(Person p) {
                                                    return p.age>20;
                                                }
                                            })
                                    .map   (new Converter<Person,String>() {
                                                public Integer convert(Person p) {
                                                    return p.name;
                                                }
                                            })
                                    .toList();

您也可以在Java8中使用它,或者在带有RetroLambda和它的gradle插件的旧java中使用它。然后您将拥有新的花式语法:

List<String> names = Queryable.from(people)
                                    .filter(p->p.age>20)
                                    .map   (p->p.name)
                                    .toList();

如果您需要运行DB查询,那么可以查看JINQ,如上所述,但RetroLambda无法对其进行反向移植,因此无法使用序列化的Lambda。

java中没有这样的功能。通过使用其他API,您将获得此功能。就像假设我们有一个包含名称和id的动物对象。我们有包含动物对象的列表对象。现在,如果我们想从列表对象中获取包含“o”的所有动物名称。我们可以编写以下查询

from(animals).where(“getName”,contains(“o”)).all();

上面的Query语句将列出名称中包含“o”字母的动物。更多信息请浏览以下博客。http://javaworldwide.blogspot.in/2012/09/linq-in-java.html

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

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

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