我有一滴树马普,我想除头10个元素外,删除所有内容。 这样做的方法是什么? 我已考虑从地图末尾删除内容,而地图面积大于10,但我不知道如何这样做。 我能否改写名单,然后回来?
增 编
我有一滴树马普,我想除头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位,无论这如何,你们的地图。
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...