scala 有没有类似 LINQ for Java 的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2008149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there something like LINQ for Java?
提问by Kb.
Started to learn LINQ with C#.
Especially LINQ to Objects and LINQ to XML.
I really enjoy the power of LINQ.
开始用C#学习LINQ。
尤其是 LINQ to Objects 和 LINQ to XML。
我真的很享受 LINQ 的强大功能。
I learned that there is something called JLINQa JavaScript implementation.
Also (as Catbert posted) Scala will have LINQ
我了解到有一种叫做JLINQ的 JavaScript 实现。
此外(如 Catbert 发布的那样)Scala 将拥有LINQ
Do you know if LINQ or something similar will be a part of Java 7?
Update: Interesting post from 2008 - LINQ for Java tool
您知道 LINQ 或类似的东西是否会成为 Java 7 的一部分吗?
更新:2008 年的有趣帖子 - LINQ for Java 工具
回答by catbert
Look at Scala, which is powerful functional programming language, but is similar to Java and runs on Java platform.
看看Scala,它是功能强大的函数式编程语言,但与 Java 类似,运行在 Java 平台上。
In Scala it is possible to use essentially the same code constructs as in LINQ, albeit without special query comprehensions syntax present in C# or VB.
在 Scala 中,可以使用与 LINQ 中基本相同的代码结构,尽管 C# 或 VB 中没有特殊的查询理解语法。
EDIT :
编辑 :
Here's an example of Scala's querying capabilities :
以下是 Scala 查询功能的示例:
// Get all StackOverflow users with more than 20,000 reputation points.
val topUsers = for{
u <- users
if u.reputation > 20000
} yield u;
println ("Users with more than 20,000 reputation:")
for (u <- topUsers) {
println u.name
}
回答by Daniel C. Sobral
It's important to note that LINQ are four things:
需要注意的是,LINQ 有四件事:
- Monadic comprehension
- Database integration
- SQL-like syntax
- AST manipulation
- 一元理解
- 数据库集成
- 类似 SQL 的语法
- AST 操作
People who just have heard of it may think of it simply as database integration. People who have worked a little with it probably think of SQL-like syntax. Those who really dug in will be aware of the monadic comprehension aspect of it, even if they don't know it for what it is.
刚刚听说过它的人可能会认为它只是数据库集成。稍微接触过它的人可能会想到类似 SQL 的语法。那些真正深入研究的人会意识到它的一元理解方面,即使他们不知道它是什么。
If one takes Scala, for example, it has monadic comprehension without the other three. There is a library called ScalaQuerywhich provides database integration through the monadic comprehension (the intrinsic ability to do so being the main reason monads are cool). Another project, called ScalaQL, I think, intends to provide pretty much the same thing, but using a compiler plugin to enhance it. I wasn't aware of Miguel Garcia's work you mentioned, but, having seen other stuff he has accomplished, I'm thrilled by it.
例如,如果以 Scala 为例,它具有 monadic comprehension,而没有其他三个。有一个名为ScalaQuery的库,它通过monadic comprehension提供数据库集成(这样做的内在能力是 monad 很酷的主要原因)。我认为另一个名为ScalaQL 的项目打算提供几乎相同的东西,但使用编译器插件来增强它。我不知道你提到的 Miguel Garcia 的作品,但是,看到他完成的其他作品,我很激动。
One doesn't need special syntax to do monadic comprehension, however. It just makes it uncluttered by boilerplate. So that aspect of it is instantly available to languages with the right level of generics support.
然而,人们不需要特殊的语法来进行 monadic comprehension。它只是让样板文件变得整洁。因此,具有正确泛型支持级别的语言可以立即使用它的这一方面。
Two things Scala doesn't do. The first is SQL-like syntax. That much can't be helped: SQL syntax looks out of place in Scala. I think it's safe to say most Scala programmers would prefer to stay with what is familiar to them -- the so-called for comprehensions.
Scala 没有做的两件事。第一个是类似 SQL 的语法。这无济于事:SQL 语法在 Scala 中显得格格不入。我认为可以肯定地说,大多数 Scala 程序员更愿意使用他们熟悉的东西——所谓的理解。
The other thing is the one I haven't discussed yet, AST manipulation. That is the ability to manipulate code that has been parsed by the compiler, but not yet transformed in byte code, granting the ability to alter it before the generation is completed.
另一件事是我还没有讨论过的,AST 操作。这就是操作已被编译器解析但尚未转换为字节码的代码的能力,从而允许在生成完成之前对其进行更改。
I think such a thing would be a boon to Scala -- heck, to any language. But, then again, I have a background as a Forth programmer, where the ability to alter code as it was being compiled was a God-given right. .Net can do it through LINQ, and so can some other languages, such as Ruby.
我认为这样的事情对 Scala 来说是一个福音——哎呀,对任何语言来说。但是,话又说回来,我有一个 Forth 程序员的背景,在那里修改正在编译的代码的能力是上帝赐予的权利。.Net 可以通过 LINQ 来完成,其他一些语言也可以,比如 Ruby。
回答by Jon Skeet
LINQ would be hard in Java due to the current lack of closures. Assuming Java 7 really doesget reasonably compact closure support and extension methods, LINQ in terms of "dot notation" should be feasible even if it doesn't get the equivalent of query expressions.
由于当前缺少闭包,LINQ 在 Java 中会很困难。假设Java 7中真正确实得到合理紧凑封闭的支持和扩展方法,LINQ中的“点号”的条款应该是可行的,即使它没有得到查询表达式的等价物。
The Google Collections Library(now at 1.0 - but to be replaced by Guavawhen that is ready) contain many of the required methods - and I wouldn't be surprised to see 101 LINQ-like APIs spring up as soon as the closure support looks reasonably final.
在谷歌集合库(现为1.0 -但被替换番石榴当准备好)含有许多必需的方法-我不会感到惊讶,看看101 LINQ类似的API尽快涌现当封闭支持长相合理最终。
I can't see (at the moment) Java getting anything like expression trees, however - so I suspect you'll be limited to LINQ to Objects unless you have custom compilation.
然而,我看不到(目前)Java 获得表达式树之类的东西 - 所以我怀疑除非您有自定义编译,否则您将仅限于 LINQ to Objects。
回答by John Feminella
Check out Quaere. It's a LINQ-like DSL for Java that you can include as a library. Example:
看看奎尔。它是一个类似于 LINQ 的 Java DSL,您可以将其作为库包含在内。例子:
// Get all StackOverflow users with more than 20,000 reputation points.
Iterable<User> topUsers = from("u").in(entityManager.entity(User.class)).
where(gt("u.getReputation()", 20000)).
select("u");
System.out.println("Users with more than 20,000 reputation:");
for (User u : topUsers) {
System.out.println(u.getName());
}
However, note that Java doesn't have a concept analogous to extension methods. Whatever's in Quaere is pretty much what you're stuck with; if you need to make your own special utilities, they'll probably have to be in separate utility classes (ick).
但是,请注意 Java 没有类似于扩展方法的概念。Quaere 中的任何东西几乎都是你所坚持的;如果您需要制作自己的特殊实用程序,它们可能必须位于单独的实用程序类中(ick)。
Additionally, because Java < 7 has no native closures, you're stuck with strings to reference things, and your IDE can't introspect those to show you problems if you mistype something. (A smarter IDE might be able to handle this shortcoming, however, if somebody were to write introspection plugins for Quaere.)
此外,由于 Java < 7 没有本机闭包,因此您只能使用字符串来引用事物,并且如果您输入错误,IDE 无法内省这些内容以向您显示问题。(但是,如果有人要为 Quaere 编写自省插件,那么更智能的 IDE 可能能够解决这个缺点。)
回答by Mario Fusco
By using the lambdajlibrary you can find the top reputation users as it follows:
通过使用lambdaj库,您可以找到顶级声誉用户,如下所示:
List<User> topUsers =
select(users, having(on(User.class).getReputation(), greaterThan(20000)));
It has some advantages respect the Quaere library because it doesn't use any magic string, it is completely type safe and in my opinion it offers a more readable DSL.
它有一些尊重 Quaere 库的优点,因为它不使用任何魔法字符串,它是完全类型安全的,在我看来,它提供了一个更具可读性的 DSL。
回答by lately worm
You could try ditingwhich implements chainable query methods on a collection.
你可以尝试谛听它实现对集合可链接的查询方法。
Integer[] values = new Integer[] {0,1,2,3,4,5,6,7,8,9,10};
Enumerable<Integer> query = new Enumerable<Integer>(values).where(new Predicate<Integer>(){
@Override
public boolean evaluate(Integer value) throws Exception {
return value % 2 == 0;
}});
for(Integer i : query)
{
System.out.write(i);
System.out.write('\n');
}
回答by Faheem Sohail
CQEngine or Collection Query Engine http://code.google.com/p/cqengine/seems extremely promising. Haven't tried it though. It allows you to build indices over collections and query them. Supposed to be very quick.
CQEngine 或集合查询引擎http://code.google.com/p/cqengine/似乎非常有前途。不过没试过。它允许您在集合上构建索引并查询它们。应该是非常快的。
回答by Mohamed Bana
Is there something as simple to use as LINQ-2-SQL on the JVM? I want to generate entities (classes) from the DB scheme using some tool and use those to interact directly with the DB. Is there an XML-free solution? I don't want to have to annotate the classes. In .NET, the tool is called sqlmetal. It's sort of a pre-processor that generates all the DB classes, so to speak.
在 JVM 上有没有像 LINQ-2-SQL 一样简单易用的东西?我想使用一些工具从 DB 方案生成实体(类),并使用它们直接与 DB 交互。是否有无 XML 的解决方案?我不想对类进行注释。在 .NET 中,该工具称为 sqlmetal。可以这么说,它是一种生成所有数据库类的预处理器。
Once this is done, I think it'll be fairly easy to use Scala's built in language constructs.
一旦完成,我认为使用 Scala 的内置语言结构将相当容易。

