Removes any keys not found in the element collection provided in this method - Java java.util

Java examples for java.util:Collection Element Remove

Description

Removes any keys not found in the element collection provided in this method

Demo Code


//package com.java2s;
import java.util.HashMap;

import java.util.Map;

public class Main {
    /**/*from ww w. jav  a2s.c  o  m*/
     * Removes any keys not found in the element collection provided in this method
     * @param map
     * @param keysToKeep
     * @return
     */
    public static <K, V> Map<K, V> filterKeys(Map<K, V> map,
            K... keysToKeep) {
        Map<K, V> filteredMap = new HashMap<K, V>();
        for (K key : keysToKeep) {
            if (map.containsKey(key)) {
                filteredMap.put(key, map.get(key));
            }
        }
        return filteredMap;
    }
}

Related Tutorials