简体   繁体   English

在java中搜索对象列表的更好方法

[英]Better way to search a list of objects in java

I have an object for servers with hostname and some sort of other fields.我有一个带有主机名和其他一些字段的服务器对象。 I'm searching my Database and get a List<Server> .我正在搜索我的数据库并获得一个List<Server> So now I have another list of orders, which have besides some other fields a field for hostname.所以现在我有另一个订单列表,除了一些其他字段之外,还有一个主机名字段。

At the moment I'm either iterating through list 1, get the hostname and then iterate trough list 2 and try to find the equal hostname, or I'm not making a list of servers but a HashMap<Hostname, Server> and look with contains if there is my hostname.目前,我要么遍历列表 1,获取主机名,然后遍历列表 2 并尝试找到相等的主机名,要么我没有制作服务器列表,而是HashMap<Hostname, Server>并查看包含是否有我的主机名。

I want to know if there is a more elegant way to do this?我想知道是否有更优雅的方法来做到这一点?

回答这个问题:当带有键的对象映射是最好的方法时,这就是我需要知道的全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有更好的方法来搜索此列表中的任意值? - Is there a better way to search for arbitrary values in this list? Java和对象初始化多个资源-有更好的方法吗? - Java and objects initialising multiple resources - is there a better way? Java搜索变量的对象列表 - Java search list of objects for variable 搜索对象列表以及递增该对象在Java中的变量的最有效方法 - Most efficient way to search list of objects and also increment variable of this object in java 检索Java中大型对象的选择属性的一种更好执行的方法 - A better performing way retrieving select attributes Collections of Large Objects in Java Java - 在对象列表中搜索两个日期之间的日期 - Java - Search a List of Objects for a date between 2 dates 一种将对象列表保存到文件中并稍后在 Java 中提取该对象列表的方法 - A way to save a list of objects into a file and pull that list of objects later in Java 获取Java中不在数据库中的对象列表的有效方法 - The efficient way to get a list of objects that are not in the database in Java 在Redis中存储Java对象列表的最佳方法 - Best way to store a list of java objects in Redis 使用Java中的对象列表迭代对象的最佳方法 - Best way to iterate an object with a list of objects in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM