Java中,搜索对象的列表内? [英] Java, searching within a list of objects?

查看:73
本文介绍了Java中,搜索对象的列表内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是有点失落的方式来实现这一目标最快的。我有一个有基本的变量属性(与getter / setter方法​​)对象的大名单,我需要做此列表中的搜索,找到匹配给定参数,该列表中的对象

I'm a bit lost on the way to make this happen the fastest. I have a large list of objects that have basic variable attributes (with getters / setters) and I need to do a search in this list to find the objects within the list that match a given parameter

我已经找到了如何做一个常规列表搜索,但我需要,例如搜索列表中的做每个对象调用的getName()的结果的价值,并得到有相匹配的结果对象我输入。

I have found how to do a regular list search but I need to, for example search for the value of the result of doing a call getName() for each object in the list and get objects that have a result that matches my input.

像下面那里的第三个参数是方法调用的结果,第二个事情就是我试图找到。

Something like below where the third argument is the result of the method call and the second is what I am trying to find.

   int index = Collections.binarySearch(myList, "value", getName());

任何意见是pciated AP $ P $

Any advice is appreciated

推荐答案

如果你只是作为一次性的操作需要找对象(S),其的getName()是一个特定的值,那么就可能没有大的魔力可能:通过列表循环,则调用GetName()每个对象上,并为那些匹配,将它们添加到您的结果列表

If you just as a one-off operation need to find the object(s) whose getName() is a particular value, then there's probably not much magic possible: cycle through the list, call getName() on each object, and for those that match, add them to your list of results.

如果的getName()是一个昂贵的操作,并有一个先验的工作了,如果给定对象绝对不会返回匹配值一些其他的方式,那么显然你可以建立在这个过滤当你循环。

If getName() is an expensive operation and there's some other way of a-priori working out if a given object definitely won't return a matching value, then obviously you can build in this 'filtering' as you cycle through.

如果您经常需要获取一个给定的getName(对象),然后持续的指数(例如,在一个HashMap)的getName()的结果 - >对象 - >匹配列表。你需要决定如何,如果需要保持这种指数,在同步与实际列表。

If you frequently need to fetch objects for a given getName(), then keep an index (e.g. in a HashMap) of [result of getName()->object -> list of matches]. You'll need to decide how and if you need to keep this "index" in synch with the actual list.

又见其他主张使用的binarySearch(),但保持维护的列表。通过这种方式,刀片是比用地图和无序列表更昂贵,但如果刀片相比,查找并不多见,那么它的只需要保持一种结构的优势。

See also the other proposition to use binarySearch() but to keep the list maintained. This way, inserts are more expensive than with a map and unsorted list, but if inserts are infrequent compared to lookups, then it has the advantage of only needing to maintain one structure.

这篇关于Java中,搜索对象的列表内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆