sup*_*ova 43 java collections linkedhashmap
我有一个有序的LinkedHashMap,我想在特定索引处添加元素,比如在地图中的第一个位置或最后一个位置.如何在LinkedHashMap中的特定位置添加元素?
即使我可以在LinkedHashMap中向FIRST或LAST位置添加元素也会有所帮助!
Mas*_*sim 21
您无法更改订单.它是insert-order(默认情况下)或access-order使用此构造函数:
public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
使用指定的初始容量,加载因子和排序模式构造一个空的LinkedHashMap实例.
参数:initialCapacity - 初始容量loadFactor - 加载因子accessOrder - 排序模式 - 访问顺序为true,插入顺序为false
抛出:IllegalArgumentException - 如果初始容量为负或负载因子为非正数
看到: LinkedHashMap
gyu*_*rix 21
您可以将此元素添加到1.或最后一个位置:
添加到最后一个地方►您只需要从地图中删除上一个条目,如下所示:
map.remove(key);
map.put(key,value);
Run Code Online (Sandbox Code Playgroud)
添加到第一个地方►它有点复杂,你需要克隆地图,清除地图,将1.值放到它上面,并将新地图放到它上面,如下所示:
我正在使用带有String键和Group(我的自定义类)值的映射:
LinkedHashMap<String, Group> newmap=(LinkedHashMap<String, Group>) map.clone();
map.clear();
map.put(key, value);
map.putAll(newm);
Run Code Online (Sandbox Code Playgroud)
如您所见,使用这些方法,您可以在地图的开头和结尾添加无限量的内容.
pul*_*ion 11
ListOrderedMap由于JDK LinkedHashMap只确保插入顺序检索,如果我们想在索引处插入,我们可以使用Apache Commons' ListOrderedMap.它听起来就是这样 - 通过一个列表来维护插入的顺序,使用相应的索引和普通的地图来插入,就像我们通常那样.以下是文档的说法:
Run Code Online (Sandbox Code Playgroud)public class ListOrderedMap<K,V> extends AbstractMapDecorator<K,V> implements OrderedMap<K,V>, Serializable装饰a
Map以确保使用List保留添加顺序以维持顺序.该命令将通过
toArray视图上的迭代器和方法使用.该订单也由MapIterator.该orderedMapIterator()方法访问迭代器,迭代器可以在地图中向前和向后迭代.此外,还提供了非接口方法来通过索引访问映射.如果对象第二次添加到Map,它将保留在迭代中的原始位置.
请注意,
ListOrderedMap它不是同步的,也不是线程安全的.如果您希望同时使用多个线程中的此映射,则必须使用适当的同步.最简单的方法是使用包装此地图Collections.synchronizedMap(Map).当没有同步的并发线程访问时,此类可能会抛出异常.请注意,
ListOrderedMap不工作IdentityHashMap,CaseInsensitiveMap或类似的地图,违反的总承包合同Map.的ListOrderedMap(或者,更精确地说,底层List)是依靠equals().这很好,只要装饰Map也是基于equals(),和hashCode(),哪个IdentityHashMap,而CaseInsensitiveMap不是:前者使用==,后者使用equals()较低的外壳键.
以下是添加到职位的实现:
/**
428 * Puts a key-value mapping into the map at the specified index.
429 * <p>
430 * If the map already contains the key, then the original mapping
431 * is removed and the new mapping added at the specified index.
432 * The remove may change the effect of the index. The index is
433 * always calculated relative to the original state of the map.
434 * <p>
435 * Thus the steps are: (1) remove the existing key-value mapping,
436 * then (2) insert the new key-value mapping at the position it
437 * would have been inserted had the remove not occurred.
438 *
439 * @param index the index at which the mapping should be inserted
440 * @param key the key
441 * @param value the value
442 * @return the value previously mapped to the key
443 * @throws IndexOutOfBoundsException if the index is out of range [0, size]
444 * @since 3.2
445 */
446 public V put(int index, final K key, final V value) {
447 if (index < 0 || index > insertOrder.size()) {
448 throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + insertOrder.size());
449 }
450
451 final Map<K, V> m = decorated();
452 if (m.containsKey(key)) {
453 final V result = m.remove(key);
454 final int pos = insertOrder.indexOf(key);
455 insertOrder.remove(pos);
456 if (pos < index) {
457 index--;
458 }
459 insertOrder.add(index, key);
460 m.put(key, value);
461 return result;
462 }
463 insertOrder.add(index, key);
464 m.put(key, value);
465 return null;
466 }
Run Code Online (Sandbox Code Playgroud)
public static <K, V> void add(LinkedHashMap<K, V> map, int index, K key, V value) {
assert (map != null);
assert !map.containsKey(key);
assert (index >= 0) && (index < map.size());
int i = 0;
List<Entry<K, V>> rest = new ArrayList<Entry<K, V>>();
for (Entry<K, V> entry : map.entrySet()) {
if (i++ >= index) {
rest.add(entry);
}
}
map.put(key, value);
for (int j = 0; j < rest.size(); j++) {
Entry<K, V> entry = rest.get(j);
map.remove(entry.getKey());
map.put(entry.getKey(), entry.getValue());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72558 次 |
| 最近记录: |