Java Map Remove removeKeyAndConvertToListOfMaps( Object obj, String key)

Here you can find the source of removeKeyAndConvertToListOfMaps( Object obj, String key)

Description

removeKeyAndConvertToListOfMaps - This method is written to meet the requirement for BML Indigo.

License

GNU General Public License

Parameter

Parameter Description
obj , Key

Return

List of Maps

Declaration

@SuppressWarnings("unchecked")
public static List<Map<Object, Object>> removeKeyAndConvertToListOfMaps(
        Object obj, String key) 

Method Source Code

//package com.java2s;
/*//ww w .  j ava 2s  .  c  om
 Pulsar
 Copyright (C) 2013-2015 eBay Software Foundation
 Licensed under the GPL v2 license.  See LICENSE for full terms.
 */

import java.util.ArrayList;

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

public class Main {
    private static final String EPL_STREAM_NAME = "name";
    private static final String EPL_STREAM_VALUE = "value";

    /**
     * removeKeyAndConvertToListOfMaps - This method is written to meet the requirement for BML Indigo. Input would be
     * key/value pair which will be converted to list of Maps for each entry. Incoming Map's Key and value would become
     * separate entry. incoming Map's Key will associate with the keyname of "Name" String Value will associate with the
     * keyname "Value" String. Also incoming key is present in the Object, that entry will be removed and will be
     * converted to ListOfMaps
     * 
     * @param obj
     *          , Key
     * @return List of Maps
     */
    @SuppressWarnings("unchecked")
    public static List<Map<Object, Object>> removeKeyAndConvertToListOfMaps(
            Object obj, String key) {
        List<Map<Object, Object>> listofMaps = null;
        if (obj instanceof Map) {
            listofMaps = new ArrayList<Map<Object, Object>>();
            Map<Object, Object> map = (Map<Object, Object>) obj;
            if (map.containsKey(key))
                map.remove(key);
            for (Entry<Object, Object> mapEntry : map.entrySet()) {
                Map<Object, Object> listmap = new HashMap<Object, Object>();
                listmap.put(EPL_STREAM_NAME, mapEntry.getKey());
                listmap.put(EPL_STREAM_VALUE, mapEntry.getValue());
                listofMaps.add(listmap);
            }
        }
        obj = null;
        return listofMaps;
    }

    /**
     * 
     * @param event
     * @param key
     * @return
     */
    public static String getValue(Map<String, Object> event, String key) {
        if (event != null && key != null) {
            return (String) event.get(key);
        }
        return null;
    }
}

Related

  1. removeFrom(Map map, K key)
  2. removeFromMap( Map map, K key, V value)
  3. removeFromMapValues(final Map map, final Object value)
  4. removeGeneric( List> list)
  5. removeHeader(Map headers, String header)
  6. removeKeys(Collection keys, Map map)
  7. removeKeysWithPrefix(Map map, String prefix)
  8. removeMapEntryFromPreferenceStoredMap(String keyOfPreference, String keyInMap)
  9. removeNamespaces(Map properties)