Java tutorial
//package com.java2s; /* * Copyright (C) 2005-2014 Alfresco Software Limited. * * This file is part of Alfresco * * Alfresco is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Alfresco is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Alfresco. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class Main { /** * This method returns a new ArrayList which is the intersection of the two List parameters, based on {@link Object#equals(Object) equality} * of their elements. * The intersection list will contain elements in the order they have in list1 and any references in the resultant list will be * to elements within list1 also. * * @return a new ArrayList whose values represent the intersection of the two Lists. */ public static <T> List<T> intersect(List<? extends T> list1, List<? extends T> list2) { if (list1 == null || list1.isEmpty() || list2 == null || list2.isEmpty()) { return Collections.emptyList(); } List<T> result = new ArrayList<T>(); result.addAll(list1); result.retainAll(list2); return result; } /** * This method returns a new HashMap which is the intersection of the two Map parameters, based on {@link Object#equals(Object) equality} * of their entries. * Any references in the resultant map will be to elements within map1. * * @return a new HashMap whose values represent the intersection of the two Maps. */ public static <K, V> Map<K, V> intersect(Map<K, V> map1, Map<K, V> map2) { if (map1 == null || map1.isEmpty() || map2 == null || map2.isEmpty()) { return Collections.emptyMap(); } // We now know neither map is null. Map<K, V> result = new HashMap<K, V>(); for (Map.Entry<K, V> item : map1.entrySet()) { V value = map2.get(item.getKey()); if (value != null && value.equals(item.getValue())) { result.put(item.getKey(), item.getValue()); } } return result; } /** * This method returns a new HashSet which is the intersection of the two Set parameters, based on {@link Object#equals(Object) equality} * of their elements. * Any references in the resultant set will be to elements within set1. * * @return a new HashSet whose values represent the intersection of the two Sets. */ public static <T> Set<T> intersect(Set<? extends T> set1, Set<? extends T> set2) { if (set1 == null || set1.isEmpty() || set2 == null || set2.isEmpty()) { return Collections.emptySet(); } Set<T> result = new HashSet<T>(); result.addAll(set1); result.retainAll(set2); return result; } public static boolean isEmpty(Map<?, ?> map) { if (map == null) { return true; } return map.isEmpty(); } public static boolean isEmpty(Collection<?> items) { if (items == null) { return true; } return items.isEmpty(); } }