[英]search object within java collection
first, I don't know if I'm organizing my data efficiently, the idea is that I have pairs of key/value. 首先,我不知道我是否可以有效地组织数据,但我的想法是我有一对键/值。
public static class Freq implements Comparable {
String term;
double frequency;
public Freq( String term, double frequency ) {
this.term = term;
this.frequency = frequency;
}
public int compareTo(Object o) {
if(this.frequency == ((Freq) o).frequency)
return 0;
else if(this.frequency > ((Freq) o).frequency)
return 1;
else
return -1;
}
Now, I'm storing such objects within a collection: List<Freq> bla = new ArrayList<Freq>()
as well as sorting it. 现在,我将此类对象存储在一个集合中:
List<Freq> bla = new ArrayList<Freq>()
以及对其进行排序。
I'm interested to search for specific objects eg Freq.name = 'Bar'
from the collection, which is sorted. 我有兴趣从集合中搜索特定对象,例如
Freq.name = 'Bar'
。 How would I do that? 我该怎么做? Or I have to iterate the whole collection.
或者我必须迭代整个集合。
or is there other more efficient ways to do this? 还是有其他更有效的方法来做到这一点?
You should use an associated collection such as a TreeMap
, which keeps its elements sorted automatically. 您应该使用关联的集合,例如
TreeMap
,以使其元素自动排序。 If you want to search sometimes based on name and sometimes on frequency, you can keep your elements in two maps at the same time, and use the suitable one for lookup. 如果要有时根据名称搜索,有时要根据频率搜索,则可以将元素同时保留在两个地图中,并使用合适的元素进行查找。
Or if for some reason you want to stick with a sorted List, you can use Collections.binarySearch()
to find elements in it. 或者,由于某种原因,如果您想使用已排序的列表,则可以使用
Collections.binarySearch()
在其中查找元素。
You can use JFilter http://code.google.com/p/jfilter/ 您可以使用JFilter http://code.google.com/p/jfilter/
JFilter is a simple and high performance open source library to query collection of Java beans. JFilter是一个简单且高性能的开源库,用于查询Java Bean的集合。
Key features 主要特征
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.