简体   繁体   English

Java集合中的搜索对象

[英]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 主要特征

  • Support of collection (java.util.Collection, java.util.Map and Array) properties. 支持集合(java.util.Collection,java.util.Map和Array)属性。
  • Support of collection inside collection of any depth. 支持集合内任何深度的集合。
  • Support of inner queries. 支持内部查询。
  • Support of parameterized queries. 支持参数化查询。
  • Can filter 1 million records in few 100 ms. 可以在几百毫秒内过滤一百万条记录。
  • Filter ( query) is given in simple json format, it is like Mangodb queries. 过滤器(查询)以简单的json格式给出,就像Mangodb查询一样。 Following are some examples. 以下是一些示例。
    • { "id":{"$le":"10"} {“ id”:{“ $ le”:“ 10”}
    • where object id property is less than equals to 10. 其中对象id属性小于等于10。
    • { "id": {"$in":["0", "100"]}} {“ id”:{“ $ in”:[“ 0”,“ 100”]}}
    • where object id property is 0 or 100. 其中对象id属性为0或100。
    • {"lineItems":{"lineAmount":"1"}} { “了LineItem”:{ “lineAmount”: “1”}}
    • where lineItems collection property of parameterized type has lineAmount equals to 1. 其中参数化类型的lineItems集合属性的lineAmount等于1。
    • { "$and":[{"id": "0"}, {"billingAddress":{"city":"DEL"}}]} {“ $ and”:[{“ id”:“ 0”},{“ billingAddress”:{“ city”:“ DEL”}}]}
    • where id property is 0 and billingAddress.city property is DEL. 其中id属性为0,billingAddress.city属性为DEL。
    • {"lineItems":{"taxes":{ "key":{"code":"GST"}, "value":{"$gt": "1.01"}}}} where lineItems collection property of parameterized type which has taxes map type property of parameteriszed type has code equals to GST value greater than 1.01. {“ lineItems”:{“ taxes”:{“ key”:{“ code”:“ GST”},“ value”:{“ $ gt”:“ 1.01”}}}}}其中lineItems集合参数类型的属性参数化类型的税收地图类型属性的代码等于GST值大于1.01。
    • {'$or':[{'code':'10'},{'skus': {'$and':[{'price':{'$in':['20', '40']}}, {'code':'RedApple'}]}}]} {'$ or':[{'code':'10'},{'skus':{'$ and':[{'price':{'$ in':['20','40']} },{'code':'RedApple'}]}}]}
    • Select all products where product code is 10 or sku price in 20 and 40 and sku code is "RedApple". 选择产品代码为10或sku价格为20和40且sku代码为“ RedApple”的所有产品。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM