Java 我可以查询列表吗?爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2848508/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:35:43  来源:igfitidea点击:

Can i query a List? Java

java

提问by TCM

Say i have

说我有

List<SomeObject> objList = new ArrayList<SomeObject>();

If someObject contains a field named id. Can we find it through some query like

如果 someObject 包含一个名为 id 的字段。我们可以通过一些查询找到它吗

objList.filter('id=2');

objList.filter('id=2');

wihout looping through the list? If not, then why? This can be so useful method and used as an alternative to write tedious for loop?

不循环遍历列表?如果不是,那为什么呢?这可以是非常有用的方法并用作编写乏味的 for 循环的替代方法吗?

采纳答案by whaley

If you want something like linq for java, check out quaere

如果你想要类似 linq for java 的东西,请查看quaere



Edit:At this point, quaere looks like it is unmaintained. With Java 7 being EOL, you should be on Java 8 whose Stream APIshould get you most of the way there when compared to Linq's dot syntax. See this pagefor some examples.

编辑:在这一点上,quaere 看起来像是没有维护的。由于 Java 7 即将停产,您应该使用 Java 8,与 Linq 的点语法相比,Java 8 的Stream API应该可以帮助您完成大部分工作。有关一些示例,请参阅此页面

回答by Martin Konecny

Even if this was a supported feature of the language, then it would be Java that would need to do the iterating, so you're back at square one (in terms on time complexity).

即使这是该语言的受支持功能,也将是 Java 需要进行迭代,因此您又回到了原点(就时间复杂度而言)。

What you're looking for is associative mapping. This can be achieved with HashMap. If you want to associate by more than one type of property for example id AND name, then you could make two HashMaps, one whose key is id and one whose key is name.

您正在寻找的是关联映射。这可以通过 HashMap 来实现。如果您想通过多种类型的属性(例如 id AND name)进行关联,那么您可以创建两个 HashMap,一个键是 id,一个键是 name。

This of course doesn't scale very well if you want to query for many properties, so the next step would be using an Object oriented database such as Hibernate which will allow you to query the database for objects exactly as in your example.

如果您想查询许多属性,这当然不能很好地扩展,因此下一步将使用面向对象的数据库,例如 Hibernate,它将允许您完全按照示例中的对象查询数据库。

回答by p4553d

short: no, you can't

简短:不,你不能

long: you can write own data structure and hash/index fields of object for some more efficient search. but this is not a list, more HashMapor so.

long:您可以编写自己的数据结构和对象的哈希/索引字段,以进行更有效的搜索。但这不是一个列表,HashMap或多或少。

回答by RoToRa

Libraries with functional, well, functionality such as functionaljavaprovide such methods.

具有功能性,功能性的库,例如functionaljava,提供了这样的方法。

You'd need to use its own Listimplementation which is incompatible to native Javas (Array)Listor convert between the two.

您需要使用List与本机 Java 不兼容的自己的实现(Array)List或在两者之间进行转换。

An Example:

一个例子:

import fj.data.Java
import fj.data.List
import fj.F

// Converting an ArrayList
fj.data.List<SomeObject> objList2 = Java.ArrayList_List().f(objList);

fj.data.List<SomeObject> filteredObjList = objList2.filter(new F<SomeObject, Boolean>() {
  Boolean f(SomeObject c) { return c.id == 2; } 
});

// Converting back to ArrayList
java.util.List<SomeObject> objList2 = Java.List_ArrayList().f(filteredObjList );

By using functionaljava's Listthrough out of your project you would avoid the converting.

通过List在您的项目中使用功能性java,您将避免转换。

回答by Low Flying Pelican

Probably this will help someone,

可能这会帮助某人,

http://code.google.com/p/joquery/

http://code.google.com/p/joquery/

this library supports following code structure,

该库支持以下代码结构,

Filter<Dto> query = CQ.<Dto>filter(testList)
    .where()
    .property("id").eq().value(1);
Collection<Dto> filtered = query.list();

回答by Der Zinger

Here How do you query object collections in Java (Criteria/SQL-like)?you can see some nice option/ I've sopped on this one actually.

这里 如何查询 Java 中的对象集合(Criteria/SQL-like)?你可以看到一些不错的选择/我实际上已经选择了这个。