English 中文(简体)
Tree 树苗
原标题:Trimming a TreeMap to n entries
  • 时间:2011-10-24 02:55:23
  •  标签:
  • java
  • hashmap

我有一滴树马普,我想除头10个元素外,删除所有内容。 这样做的方法是什么? 我已考虑从地图末尾删除内容,而地图面积大于10,但我不知道如何这样做。 我能否改写名单,然后回来?

增 编

问题回答

<编码> HashMaps t没有开始或结束——没有顺序。 您对这些项目的顺序与你插入这些项目的顺序完全无关。 如果你可以使用连接式HashMap,那么这一类别实际上确实保持了插入这些物品的顺序。 然后,在您在头10年发出催复通知后,你可以简单地在每件物品的发件人上打上remove(<>/code>>。

这里是想的。 或许可以制作一个范围限于10个元素的地图级,并复制/建造你的有限地图部分:

package main;

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.common.collect.Maps;

public class HashMap10<K, V> extends LinkedHashMap<K, V> {

    private static final long serialVersionUID = -4943383947326287590L;

    static Map<Integer, String> x = Maps.newHashMap();

    public HashMap10() {
        super();
    }

    public HashMap10(int initialCapacity, float loadFactor, boolean accessOrder) {
        super(initialCapacity, loadFactor, accessOrder);
    }

    public HashMap10(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }

    public HashMap10(int initialCapacity) {
        super(initialCapacity);
    }

    public HashMap10(Map<? extends K, ? extends V> m) {
        putAll(m);
    }

    @Override
    public V put(K key, V value) {
        if (this.size() == 10) {
            return null;
        }
        return super.put(key, value);
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {
        for (java.util.Map.Entry<? extends K, ? extends V> x : m.entrySet()) {
            put(x.getKey(), x.getValue());
        }
    }

}

通过在有限规模的地图上添加100个内容,一次用多层电话进行测试,一次使用建筑商:

package main;

import java.util.HashMap;
import java.util.Map.Entry;

public class TestIt {

    /**
     * @param args
     */
    public static void main(String[] args) {
        HashMap10<Integer, String> map10 = new HashMap10<Integer, String>();

        for (int i = 1; i < 99; i++) {
            map10.put(i, Integer.toString(i + 100));
        }

        for (Entry<Integer, String> x : map10.entrySet()) {
            System.out.println(x.getKey() + "->" + x.getValue());
        }
        System.out.println("");
        System.out.println("");

        HashMap<Integer, String> mapUnlimited = new HashMap<Integer, String>();

        for (int i = 1; i < 99; i++) {
            mapUnlimited.put(i, Integer.toString(i + 200));
        }

        HashMap10<Integer, String> anotherMap10 = new HashMap10<Integer, String>(
                mapUnlimited);
        for (Entry<Integer, String> x : anotherMap10.entrySet()) {
            System.out.println(x.getKey() + "->" + x.getValue());
        }

    }

}

Dump the maps, and you only have maps of 10 elements, whether you added the elements one at a time, or built the map with a constructor:

1->101
2->102
3->103
4->104
5->105
6->106
7->107
8->108
9->109
10->110


1->201
2->202
3->203
4->204
5->205
6->206
7->207
8->208
9->209
10->210

我没有花任何时间来进行防御性方案拟定工作,检查失败者,这是一件事。 这里的想法是,我不知道或看你原来的地图。 链接地图。 树木地图。 定期地图。 不管这些内容的顺序如何,都界定了“头10项内容”的含义,我的地图类别只储存头10项内容,然后忽略任何内容。 现在,你们有头10位,无论这如何,你们的地图。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...