Get sub Map from key collection - Java java.util

Java examples for java.util:Collection Sub Collection

Description

Get sub Map from key collection

Demo Code


//package com.java2s;

import java.util.*;

public class Main {

    public static <K, V> Map<K, V> subMap(Map<K, V> map, Collection<K> keys) {
        Map<K, V> subMap = new HashMap<K, V>(keys.size());
        for (K key : keys) {
            V value = map.get(key);//  w w  w  .ja v a 2  s .  c  o  m
            if (value != null) {
                subMap.put(key, value);
            }
        }
        return subMap;
    }
}

Related Tutorials