如何在树形图中获取条目的子集?

时间:2012-09-06 07:07:57

标签: java xpages

我有一个

TreeMap map = new TreeMap();

根据选择条件存储视图中文档的值。 map将创建日期存储为键,将文档值存储为对象。

在对象中,我有一个"形式"领域。文件可以有不同的形式(备忘录,通知,传真等)

我有一个返回allEntries的方法。没关系,按预期工作。

我现在想在表单字段上进行选择,只返回地图中文档的一部分。

有人能指出我正确的方向如何做到这一点?

4 个答案:

答案 0 :(得分:4)

由于您想要选择不是地图关键字的东西,您必须遍历所有项目。

如果您可以使用外部库,则可以使用Google Collections2.filter()中的guava libraries。即。

TreeMap<Date, Document> map = ...;
final String formValue = "notice";
Collection<Document> result = Collections2.filter(map.values(), new Predicate<Document>() {
    public boolean apply(Document input) {
        return formValue .equals(input.getForm());
    }
}

答案 1 :(得分:0)

您可以使用2张地图存储数据。

一个简单的Hashmap足以存储对应于memo-notice等的id,并且作为一个值包含一个Treemap,其中包含当前格式的所有对应数据。

答案 2 :(得分:0)

你可以 1)浏览所有值并将这些匹配标准放入新集合中 2)存储其他集合(单独的变量或Map&gt;(或类似的东西,考虑使用set而不是List)

1)检索速度较慢,2)删除速度较慢,消耗的内存较多

答案 3 :(得分:0)

番石榴图书馆工作,但有点矫枉过正。所以我最终得到了以下

TreeMap<String, Object> mapFiltered = new TreeMap<String, Object>();
LinkedList<CommunicationDocumentEntry> map = new LinkedList<CommunicationDocumentEntry>();

    public Collection<Object> getAllEntries(String frm, boolean order) {
    this.mapFiltered.clear();

    Iterator<CommunicationDocumentEntry> itr = this.map.iterator();
    while (itr.hasNext()) {
        CommunicationDocumentEntry entry = itr.next();
        if (EMPTY_STRING.equals(frm) || frm == null) {
            this.mapFiltered.put(entry.getDateCreated(), entry.getEntry());
        } else {
            if (entry.getForm().toUpperCase().equals(frm.toUpperCase())) {
                this.mapFiltered.put(entry.getDateCreated(), entry.getEntry());
            }
        }
    }

    if (order) {
        return this.mapFiltered.descendingMap().values();
    } else {
        return this.mapFiltered.values();
    }
}

当然不完美,但对我有用......