Agi*_*Jon 802
没有像LINQ for Java那样的东西.
...
编辑
现在使用Java 8,我们将介绍Stream API,这在处理集合时是类似的事情,但它与Linq并不完全相同.
如果它是您正在寻找的ORM,例如Entity Framework,那么您可以尝试Hibernate
:-)
19W*_*S85 155
有另一种解决方案,Coollection.
Coolection并没有假装成新的lambda,但是我们被旧的遗留Java项目所包围,这个lib将会有所帮助.它的使用和扩展非常简单,仅涵盖迭代最常用的集合操作,如下所示:
from(people).where("name", eq("Arthur")).first();
from(people).where("age", lessThan(20)).all();
from(people).where("name", not(contains("Francine"))).all();
Run Code Online (Sandbox Code Playgroud)
Bre*_*yan 145
Lambdas现在在Java 8中以JSR-335的形式提供- 用于JavaTM编程语言的Lambda表达式
更新:JDK8现已发布,其中包含项目lambda.值得抓取目前仍在MEAP中的Java 8 in Action副本.
阅读Brian Goetz关于lambdas的文章,以便理解lambda在JDK8中的实现方式,同时还要了解流,内部迭代,短路和构造函数引用.另请参阅上面的JSR以获取更多示例.
我写了一篇关于在JDK8中使用lambdas的一些优点的博客,名为The Power of the Arrow,NetBeans 8也非常支持将构造转换为JDK8,我还在博客中介绍了如何使用NetBeans迁移到JDK 8.
Mar*_*sco 119
您可以使用lambdaj库以更易读的方式选择集合中的项目(以及更多)
https://code.google.com/archive/p/lambdaj/
它比Quaere库有一些优势,因为它不使用任何魔术字符串,它完全是类型安全的,在我看来它提供了更易读的DSL.
Răz*_*nda 49
LINQ to Objects - JAVA 8添加了Stream API,它增加了对值流的功能样式操作的支持:
LINQ to SQL/NHibernate/etc. (数据库查询) - 一种选择是使用JINQ,它也使用新的JAVA 8功能,于2014年2月26日在Github上发布:https://github.com/my2iu/Jinq
Jinq为开发人员提供了一种用Java编写数据库查询的简单而自然的方法.您可以将数据库数据视为存储在集合中的普通Java对象.您可以迭代它们并使用普通的Java命令对它们进行过滤,并且所有代码都将自动转换为优化的数据库查询.最后,LINQ样式的查询可用于Java!
JINQ项目现场:http://www.jinq.org/
我试过google的guava-libraries.它有一个FluentIterable我认为接近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();
}
});
Run Code Online (Sandbox Code Playgroud)
似乎是一个广泛的Java库.当然不像LINQ那样简洁但看起来很有趣.
https://code.google.com/p/joquery/
支持不同的可能性,
鉴于收藏,
Collection<Dto> testList = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
类型,
class Dto
{
private int id;
private String text;
public int getId()
{
return id;
}
public int getText()
{
return text;
}
}
Run Code Online (Sandbox Code Playgroud)
过滤
Java 7
Filter<Dto> query = CQ.<Dto>filter(testList)
.where()
.property("id").eq().value(1);
Collection<Dto> filtered = query.list();
Run Code Online (Sandbox Code Playgroud)
Java 8
Filter<Dto> query = CQ.<Dto>filter(testList)
.where()
.property(Dto::getId)
.eq().value(1);
Collection<Dto> filtered = query.list();
Run Code Online (Sandbox Code Playgroud)
也,
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"});
Run Code Online (Sandbox Code Playgroud)
排序(也适用于Java 7)
Filter<Dto> query = CQ.<Dto>filter(testList)
.orderBy()
.property(Dto::getId)
.property(Dto::getName)
Collection<Dto> sorted = query.list();
Run Code Online (Sandbox Code Playgroud)
分组(也适用于Java 7)
GroupQuery<Integer,Dto> query = CQ.<Dto,Dto>query(testList)
.group()
.groupBy(Dto::getId)
Collection<Grouping<Integer,Dto>> grouped = query.list();
Run Code Online (Sandbox Code Playgroud)
连接(也可用于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<>();
Run Code Online (Sandbox Code Playgroud)
可以加入像,
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();
Run Code Online (Sandbox Code Playgroud)
表达式
Filter<Dto> query = CQ.<Dto>filter()
.from(testList)
.where()
.exec(s -> s.getId() + 1).eq().value(2);
Run Code Online (Sandbox Code Playgroud)
你可以试试我的库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();
Run Code Online (Sandbox Code Playgroud)
您也可以在 Java8 中使用它,或者在带有RetroLambda 的旧 Java 中使用它,它是gradle plugin,那么您将拥有新的花哨语法:
List<String> names = Queryable.from(people)
.filter(p->p.age>20)
.map (p->p.name)
.toList();
Run Code Online (Sandbox Code Playgroud)
如果您需要运行数据库查询,那么您可以查看 JINQ,如上所述,但它不能由 RetroLambda 向后移植,不能使用序列化的 lambda。
| 归档时间: |
|
| 查看次数: |
269277 次 |
| 最近记录: |