有没有类似 LINQ for Java 的东西?

发布于 2024-08-17 05:35:42 字数 499 浏览 4 评论 0原文

开始用C#学习LINQ。
特别是 LINQ to Objects 和 LINQ to XML。
我真的很喜欢 LINQ 的强大功能。

我了解到有一种名为 JLINQ 的 JavaScript 实现.
另外(正如 Catbert 发布的)Scala 将有 LINQ

你知道吗?知道 LINQ 或类似的东西是否会成为 Java 7 的一部分?

更新:2008 年的有趣帖子 - LINQ for Java 工具

Started to learn LINQ with C#.
Especially LINQ to Objects and LINQ to XML.
I really enjoy the power of LINQ.

I learned that there is something called JLINQ a JavaScript implementation.
Also (as Catbert posted) Scala will have 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

写下不归期 2024-08-24 05:35:42

看看Scala,它是一种功能强大的函数式编程语言,但与Java类似,运行在Java平台上。

在 Scala 中,可以使用与 LINQ 中基本相同的代码结构,尽管 C# 或 VB 中没有特殊的查询理解语法。

编辑:

这是 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
}

Look at Scala, which is powerful functional programming language, but is similar to Java and runs on Java platform.

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.

EDIT :

Here's an example of Scala's querying capabilities :

// 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
}
夜吻♂芭芘 2024-08-24 05:35:42

需要注意的是,LINQ 包含四件事:

  • Monadic 理解
  • 数据库集成
  • 类似 SQL 的语法
  • AST 操作

刚刚听说过它的人可能会认为它只是数据库集成。稍微接触过它的人可能会想到类似 SQL 的语法。那些真正深入研究的人会意识到它的单子理解方面,即使他们不知道它是什么。

例如,如果采用 Scala,它就具有一元理解力,而无需其他三个。有一个名为 ScalaQuery 的库,它通过单子理解(这样做的内在能力)提供数据库集成这是 monad 很酷的主要原因)。我认为另一个名为 ScalaQL 的项目旨在提供几乎相同的事情,但使用编译器插件来增强它。我不知道你提到的米格尔·加西亚的工作,但是,在看到他所取得的其他成就后,我对此感到兴奋。

然而,不需要特殊的语法来进行一元理解。它只是让样板变得整洁。因此,具有适当级别的泛型支持的语言可以立即使用它的这一方面。

有两件事是 Scala 不做的。第一个是类似 SQL 的语法。这也是没办法的事:SQL 语法在 Scala 中看起来不合适。我认为可以肯定地说,大多数 Scala 程序员更愿意保留他们熟悉的东西——所谓的推导式。

另一件事是我还没有讨论过的,AST 操作。这是操作已被编译器解析但尚未转换为字节码的代码的能力,从而允许在生成完成之前对其进行更改。

我认为这样的事情对 Scala 来说是一个福音——哎呀,对任何语言都是如此。但是,话又说回来,我有 Forth 程序员的背景,在编译代码时更改代码的能力是上帝赋予的权利。 .Net 可以通过 LINQ 来做到这一点,其他一些语言也可以,比如 Ruby。

It's important to note that LINQ are four things:

  • Monadic comprehension
  • Database integration
  • SQL-like syntax
  • AST manipulation

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.

If one takes Scala, for example, it has monadic comprehension without the other three. There is a library called ScalaQuery which 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.

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.

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.

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.

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.

樱娆 2024-08-24 05:35:42

由于目前缺乏闭包,LINQ 在 Java 中会很困难。假设 Java 7 确实获得了相当紧凑的闭包支持和扩展方法,那么“点表示法”方面的 LINQ 应该是可行的,即使它没有获得查询表达式的等效项。

Google Collections Library(目前为 1.0 - 但将被 Guava 当准备好时)包含许多必需的方法 - 我不会感到惊讶地看到一旦闭包支持看起来相当最终,就会出现 101 个类似 LINQ 的 API。

然而,我(目前)看不到 Java 获得像表达式树这样的东西 - 所以我怀疑除非您有自定义编译,否则您将仅限于 LINQ to Objects。

LINQ would be hard in Java due to the current lack of closures. Assuming Java 7 really does get 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.

The Google Collections Library (now at 1.0 - but to be replaced by Guava when 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.

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.

风为裳 2024-08-24 05:35:42

查看Quaere。它是一个类似于 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());
}

但是请注意,Java 没有类似于扩展方法的概念。无论Quaere 中有什么,你都会被困住。如果您需要创建自己的特殊实用程序,它们可能必须位于单独的实用程序类中(ick)。

另外,因为Java < 7 没有本机闭包,您只能使用字符串来引用事物,并且如果您输入错误,您的 IDE 无法内省这些内容以向您显示问题。 (但是,如果有人为 Quaere 编写内省插件,更智能的 IDE 可能能够解决这个缺点。)

Check out Quaere. It's a LINQ-like DSL for Java that you can include as a library. Example:

// 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).

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

苍暮颜 2024-08-24 05:35:42

通过使用 lambdaj 库,您可以找到信誉最高的用户,如下所示:

List<User> topUsers = 
    select(users, having(on(User.class).getReputation(), greaterThan(20000)));

它有一些优点尊重 Quaere 库,因为它不使用任何魔术字符串,它是完全类型安全的,并且在我看来它提供了更具可读性的 DSL。

By using the lambdaj library you can find the top reputation users as it follows:

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.

恋竹姑娘 2024-08-24 05:35:42

您可以尝试diting,它在集合上实现可链接查询方法。

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

You could try diting which 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');
   }
骄傲 2024-08-24 05:35:42

CQEngine 或集合查询引擎 http://code.google.com/p/cqengine/ 似乎非常有前途。不过还没试过。它允许您在集合上构建索引并查询它们。应该会很快。

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.

笑叹一世浮沉 2024-08-24 05:35:42

有没有像 JVM 上的 LINQ-2-SQL 一样简单易用的东西?我想使用某种工具从数据库方案生成实体(类),并使用它们直接与数据库交互。有无 XML 的解决方案吗?我不想对课程进行注释。在 .NET 中,该工具称为 sqlmetal。可以说,它是一种生成所有数据库类的预处理器。

一旦完成,我认为使用 Scala 的内置语言结构将会相当容易。

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.

Once this is done, I think it'll be fairly easy to use Scala's built in language constructs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文