按属性或类型从一组对象中选择对象

时间:2011-11-13 18:58:43

标签: java search data-structures

想象一下,我有一组复杂的对象,假设它们有名字,姓氏和颜色。

我想检索该组中所有红色对象的选择,并使用以“K”开头的姓氏。

java是否包含一个数据结构,我可以存储这些对象,允许这种选择?是否有一个优雅的解决方案,不涉及写一堆for循环?

我想在不使用任何java数据库库的情况下执行此操作。

3 个答案:

答案 0 :(得分:0)

AFAIK,Java中没有数据结构可以让你开箱即用。你需要像inverted index之类的东西来完成这项工作。 Lucene允许您执行您刚才非常有效的所有搜索操作。我相信它也有一个内存存储,所以它就足够了。

答案 1 :(得分:0)

项目http://www.glazedlists.com/有一个GroupingList,可让您通过自己的Comparator对List的元素进行分组。这里是GroupingList的链接:http://publicobject.com/glazedlists/glazedlists-1.8.0/api/ca/odell/glazedlists/GroupingList.html

答案 2 :(得分:0)

这是我头顶的一个示例(我没有实现删除方法):

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class IndexedCollection<E> extends ArrayList<E> {
    private Map<String, Map<Object, Set<E>>> indices = new HashMap<String,  Map<Object, Set<E>>>();

    public IndexedCollection(String...indices) {
        for (String index: indices) {
            this.indices.put(index, new HashMap<Object, Set<E>>());
        }
    }

    public boolean add(E entry) {
        try {
                Map<String, Object> objectIndices = collectIndices(entry);

                for (Entry<String, Object> objectIndex: objectIndices.entrySet()) {
                    Map<Object, Set<E>> map = this.indices.get(objectIndex.getKey());
                    Set<E> set = map.get(objectIndex.getValue());

                    if (set == null) {
                        set = new HashSet<E>();
                        map.put(objectIndex.getValue(), set);
                    }

                    set.add(entry);
                }

                return super.add(entry);
        }
        catch (Exception e)
        {
            throw new RuntimeException("Failed to collect object indices", e);
        }
    };

    public Set<E> findByIndex(String name, Object value) {
        Map<Object, Set<E>> index = this.indices.get(name);

        if (index != null)
            return index.get(value);

        return null;
    }

    public Set<E> findByIndices(Index...indices) {
        Set<E> results = null;

        for (Index index: indices) {
            Set<E> tmp = findByIndex(index.name, index.value);

            if (tmp.size() == 0)
                return null;

            if (results == null)
                results = tmp;
            else {
                Set<E> newResult = new HashSet<E>();
                for (E value: results) {
                    if (tmp.contains(value))
                        newResult.add(value);
                }

                results = newResult;
}
        }

        return results;
    }

    private Map<String, Object> collectIndices(E e) throws IllegalArgumentException, IllegalAccessException {
        Map<String, Object> indices = new HashMap<String, Object>();

        Field[] fields = e.getClass().getDeclaredFields();

        for (Field field: fields) {
            if (this.indices.containsKey(field.getName())) {
                boolean accessible = field.isAccessible();
                field.setAccessible(true);
                indices.put(field.getName(), field.get(e));
                field.setAccessible(accessible);
            }
        }

        return indices;
    }

    public static class Index {
        private String name;
        private Object value;

        public Index(String name, Object value) {
            this.name = name;
            this.value = value;
        }
    }
}
相关问题