Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2008 - 2014 Red Hat, Inc. and others. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Xavier Coulon - Initial API and implementation ******************************************************************************/ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /** * Compute the intersection of elements between the 2 given maps * * @param control * the control collection * @param test * the test collection * @return the elements of the control map that whose keys are part of the * test map. The process works with keys and does not compare the * values. */ public static <K, V> Map<K, V> intersection(final Map<K, V> control, final Map<K, V> test) { if (control == null) { return null; } if (test == null) { return control; } Collection<K> keys = intersection(control.keySet(), test.keySet()); Map<K, V> result = new HashMap<K, V>(); for (K key : keys) { result.put(key, control.get(key)); } return result; } /** * Compute the intersection of elements between the 2 given collections * * @param control * the control collection * @param test * the test collection * @return the elements of the control collection that are also part of the * test collection. */ public static <T> List<T> intersection(final Collection<T> control, final Collection<T> test) { if (control == null) { return null; } List<T> result = new ArrayList<T>(control); if (test != null) { result.retainAll(test); } return result; } }