Java Map Put putAt(LinkedHashMap map, K key, V value, int pos)

Here you can find the source of putAt(LinkedHashMap map, K key, V value, int pos)

Description

put At

License

Open Source License

Declaration

public static <K, V> void putAt(LinkedHashMap<K, V> map, K key, V value, int pos) 

Method Source Code

//package com.java2s;

import java.util.Iterator;
import java.util.LinkedHashMap;

import java.util.Map.Entry;

public class Main {
    public static <K, V> void putAt(LinkedHashMap<K, V> map, K key, V value, int pos) {
        Iterator<Entry<K, V>> ei = map.entrySet().iterator();
        LinkedHashMap<K, V> pre = new LinkedHashMap<>();
        LinkedHashMap<K, V> post = new LinkedHashMap<>();

        for (int i = 0; i < pos; i++) {
            if (!ei.hasNext())
                break;
            Entry<K, V> tmpE = ei.next();
            pre.put(tmpE.getKey(), tmpE.getValue());
        }/*from  www. ja v a  2s . co  m*/
        // skip element at pos
        if (ei.hasNext())
            ei.next();
        while (ei.hasNext()) {
            Entry<K, V> tmpE = ei.next();
            post.put(tmpE.getKey(), tmpE.getValue());
        }

        map.clear();
        map.putAll(pre);
        map.put(key, value);
        map.putAll(post);
    }
}

Related

  1. putAllObjects(Map targetMap, Map sourceMap)
  2. putAllRecursively(Map destination, Map source)
  3. putAllTransposed( Map> source_key_valueSet, Map output_value_key)
  4. putAsCollection(K key, V value, Map map)
  5. putAsStringIfNotNull(Map properties, String key, Object value)
  6. putBoolean(Map properties, Object key, boolean value)
  7. putByFullKey(Map map, String key, Object value)
  8. putCheckedObjectInInnerMap(Map> mapValues, A key, K innerKey, V obj)
  9. putClassToMap(String extensionId, String key, Class className)