Java tutorial
//package com.java2s; /* * $Id$ * * This is a program for Language Grid Core Node. This combines multiple language resources and provides composite language services. * Copyright (C) 2005-2008 NICT Language Grid Project. * * This program 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 2.1 of the License, or (at * your option) any later version. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; public class Main { /** * * */ public static <T> boolean equalsAsSet(Collection<T> value1, Collection<T> value2) { Set<Object> s1 = new HashSet<Object>(); for (Object v : value1) { s1.add(v); } Set<Object> s2 = new HashSet<Object>(); for (Object v : value2) { s2.add(v); } return s1.equals(s2); } /** * * */ public static <T> boolean equalsAsSet(Collection<T> value1, Collection<T> value2, Class<T> clazz, String equalsMethodName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Method m = null; try { m = clazz.getMethod(equalsMethodName, clazz); } catch (NoSuchMethodException e) { m = clazz.getMethod(equalsMethodName, Object.class); } return equalsAsSet(value1, value2, m); } /** * * */ public static <T> boolean equalsAsSet(Collection<T> value1, Collection<T> value2, Method equalsMethod) throws IllegalAccessException, InvocationTargetException { if (!equalsMethod.getReturnType().equals(boolean.class)) { return false; } List<T> s1 = new LinkedList<T>(); for (T v : value1) { s1.add(v); } for (T v : value2) { Iterator<T> i2 = s1.iterator(); boolean found = false; while (i2.hasNext()) { if (((Boolean) equalsMethod.invoke(v, i2.next())).booleanValue()) { i2.remove(); found = true; break; } } if (!found) return false; } return s1.size() == 0; } }