[英]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.