Java/Collections Data Structure/Collection
Содержание
- 1 A combination of two collections into a collection
- 2 Append, filter and get the size of collections
- 3 A utility class for working with attribute and parameter collections used by Spring Web FLow
- 4 Check whether the given Collection contains the given element instance.
- 5 Clone an array
- 6 Clones a map and prefixes the keys in the clone
- 7 Clones the lhs map and add all things from the rhs map.
- 8 Deep clone collection: Returns a new collection containing clones of all the items in the specified collection.
- 9 Disjoint two collections
- 10 Find a value of the given type in the given Collection
- 11 Get the difference of two collections
- 12 Is collection is empty, and all of its elements are empty
- 13 Return the first element in "candidates" that is contained in source
- 14 Return true if any element in "candidates" is contained in source
- 15 Simple implementation of a generic Bag
- 16 The collection related utilities
- 17 Whether Collection only contains a single unique object
A combination of two collections into a collection
<source lang="java">
/* DualCollection.java {{IS_NOTE
Purpose: Description: History: Sun Sep 2 21:29:38 2007, Created by tomyeh
}}IS_NOTE Copyright (C) 2007 Potix Corporation. All Rights Reserved. {{IS_RIGHT
This program is distributed under GPL Version 3.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
- /
import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.AbstractCollection; /**
* A combination of two collections into a collection. * * @author tomyeh * @since 3.0.0 */
public class DualCollection extends AbstractCollection implements java.io.Serializable {
private final Collection _first, _second; /** Returns a collection by combining two collections. * It checks whether any of them is null, or equals. And, returns * the non-null one if another is null. * If both null, it returns null. */ public static final Collection combine(Collection first, Collection second) { if (first == second) //we don"t use equals to have better performance return first; if (first != null) if (second != null) return new DualCollection(first, second); else return first; else return second; } /** Constructor. * It is better to use {@link #combine} instead of this method * since it checks whether any of them is null or equals. */ public DualCollection(Collection first, Collection second) { _first = first != null ? first: Collections.EMPTY_LIST; _second = second != null ? second: Collections.EMPTY_LIST; } //Collection// public int size() { return _first.size() + _second.size(); } public Iterator iterator() { return new Iter(); } private class Iter implements Iterator { private Iterator _it; private boolean _bSecond; private Iter() { _it = _first.iterator(); } public boolean hasNext() { return _it.hasNext() || (!_bSecond && !_second.isEmpty()); } public Object next() { if (!_bSecond && !_it.hasNext()) { _it = _second.iterator(); _bSecond = true; } return _it.next(); } public void remove() { _it.remove(); } }
}
</source>
Append, filter and get the size of collections
<source lang="java">
/**
* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import org.w3c.dom.NodeList; /**
* A number of helper methods for working with collections * * @version $Revision: 747062 $ */
public final class CollectionHelper {
/** * Utility classes should not have a public constructor. */ private CollectionHelper() { } /** * Returns the size of the collection if it can be determined to be a collection * * @param value the collection * @return the size, or null if not a collection */ public static Integer size(Object value) { if (value != null) { if (value instanceof Collection) { Collection collection = (Collection)value; return collection.size(); } else if (value instanceof Map) { Map map = (Map)value; return map.size(); } else if (value instanceof Object[]) { Object[] array = (Object[])value; return array.length; } else if (value.getClass().isArray()) { return Array.getLength(value); } else if (value instanceof NodeList) { NodeList nodeList = (NodeList)value; return nodeList.getLength(); } } return null; } /** * Sets the value of the entry in the map for the given key, though if the * map already contains a value for the given key then the value is appended * to a list of values. * * @param map the map to add the entry to * @param key the key in the map * @param value the value to put in the map */ @SuppressWarnings("unchecked") public static void appendValue(Map map, Object key, Object value) { Object oldValue = map.get(key); if (oldValue != null) { List list; if (oldValue instanceof List) { list = (List)oldValue; } else { list = new ArrayList(); list.add(oldValue); } list.add(value); } else { map.put(key, value); } } /** * Filters the given list to skip instanceof filter objects. * * @param list the list * @param filters objects to skip * @return a new list without the filtered objects */ @SuppressWarnings("unchecked") public static List filterList(List list, Object... filters) { List answer = new ArrayList(); for (Object o : list) { for (Object filter : filters) { if (!o.getClass().isInstance(filter)) { answer.add(o); } } } return answer; } public static String collectionAsCommaDelimitedString(String[] col) { if (col == null || col.length == 0) { return ""; } return collectionAsCommaDelimitedString(Arrays.asList(col)); } public static String collectionAsCommaDelimitedString(Collection col) { if (col == null || col.isEmpty()) { return ""; } StringBuilder sb = new StringBuilder(); Iterator it = col.iterator(); while (it.hasNext()) { sb.append(it.next()); if (it.hasNext()) { sb.append(","); } } return sb.toString(); }
}
</source>
A utility class for working with attribute and parameter collections used by Spring Web FLow
<source lang="java">
/*
* Copyright 2004-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
import java.io.Serializable; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; import java.util.List; /**
* A utility class for working with attribute and parameter collections used by Spring Web FLow. * * @author Keith Donald * @author Erwin Vervaet */
public class CollectionUtils {
/** * The shared, singleton empty iterator instance. */ public static final Iterator EMPTY_ITERATOR = new EmptyIterator(); /** * Private constructor to avoid instantiation. */ private CollectionUtils() { } /** * Factory method that adapts an enumeration to an iterator. * @param enumeration the enumeration * @return the iterator */ public static Iterator toIterator(Enumeration enumeration) { return new EnumerationIterator(enumeration); }
/** * Add all given objects to given target list. No duplicates will be added. The contains() method of the given * target list will be used to determine whether or not an object is already in the list. * @param target the collection to which to objects will be added * @param objects the objects to add * @return whether or not the target collection changed */ public static boolean addAllNoDuplicates(List target, Object[] objects) { if (objects == null || objects.length == 0) { return false; } else { boolean changed = false; for (int i = 0; i < objects.length; i++) { if (!target.contains(objects[i])) { target.add(objects[i]); changed = true; } } return changed; } } /** * Iterator iterating over no elements (hasNext() always returns false). */ private static class EmptyIterator implements Iterator, Serializable { private EmptyIterator() { } public boolean hasNext() { return false; } public Object next() { throw new UnsupportedOperationException("There are no elements"); } public void remove() { throw new UnsupportedOperationException("There are no elements"); } } /** * Iterator wrapping an Enumeration. */ private static class EnumerationIterator implements Iterator { private Enumeration enumeration; public EnumerationIterator(Enumeration enumeration) { this.enumeration = enumeration; } public boolean hasNext() { return enumeration.hasMoreElements(); } public Object next() { return enumeration.nextElement(); } public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException("Not supported"); } }
}
</source>
Check whether the given Collection contains the given element instance.
<source lang="java">
import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; /*
* Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/**
* Miscellaneous collection utility methods. * Mainly for internal use within the framework. * * @author Juergen Hoeller * @author Rob Harrop * @since 1.1.3 */
abstract class CollectionUtils {
/** * Check whether the given Collection contains the given element instance.*
Enforces the given instance to be present, rather than returning
* true
for an equal element as well.
* @param collection the Collection to check
* @param element the element to look for
* @return true
if found, false
else
*/
public static boolean containsInstance(Collection collection, Object element) {
if (collection != null) {
for (Iterator it = collection.iterator(); it.hasNext();) {
Object candidate = it.next();
if (candidate == element) {
return true;
}
}
}
return false;
}
/**
* Return true
if the supplied Collection is null
* or empty. Otherwise, return false
.
* @param collection the Collection to check
* @return whether the given Collection is empty
*/
public static boolean isEmpty(Collection collection) {
return (collection == null || collection.isEmpty());
}
/**
* Return true
if the supplied Map is null
* or empty. Otherwise, return false
.
* @param map the Map to check
* @return whether the given Map is empty
*/
public static boolean isEmpty(Map map) {
return (map == null || map.isEmpty());
}
/**
* Return true
if any element in "candidates
" is
* contained in "source
"; otherwise returns false
.
* @param source the source Collection
* @param candidates the candidates to search for
* @return whether any of the candidates has been found
*/
public static boolean containsAny(Collection source, Collection candidates) {
if (isEmpty(source) || isEmpty(candidates)) {
return false;
}
for (Iterator it = candidates.iterator(); it.hasNext();) {
if (source.contains(it.next())) {
return true;
}
}
return false;
}
/**
* Return the first element in "candidates
" that is contained in
* "source
". If no element in "candidates
" is present in
* "source
" returns null
. Iteration order is
* {@link Collection} implementation specific.
* @param source the source Collection
* @param candidates the candidates to search for
* @return the first present object, or null
if not found
*/
public static Object findFirstMatch(Collection source, Collection candidates) {
if (isEmpty(source) || isEmpty(candidates)) {
return null;
}
for (Iterator it = candidates.iterator(); it.hasNext();) {
Object candidate = it.next();
if (source.contains(candidate)) {
return candidate;
}
}
return null;
}
/**
* Find a value of the given type in the given Collection.
* @param collection the Collection to search
* @param type the type to look for
* @return a value of the given type found, or null
if none
* @throws IllegalArgumentException if more than one value of the given type found
*/
public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException {
if (isEmpty(collection)) {
return null;
}
Class typeToUse = (type != null ? type : Object.class);
Object value = null;
for (Iterator it = collection.iterator(); it.hasNext();) {
Object obj = it.next();
if (typeToUse.isInstance(obj)) {
if (value != null) {
throw new IllegalArgumentException("More than one value of type [" + typeToUse.getName() + "] found");
}
value = obj;
}
}
return value;
}
/**
* Determine whether the given Collection only contains a single unique object.
* @param collection the Collection to check
* @return true
if the collection contains a single reference or
* multiple references to the same instance, false
else
*/
public static boolean hasUniqueObject(Collection collection) {
if (isEmpty(collection)) {
return false;
}
boolean hasCandidate = false;
Object candidate = null;
for (Iterator it = collection.iterator(); it.hasNext();) {
Object elem = it.next();
if (!hasCandidate) {
hasCandidate = true;
candidate = elem;
}
else if (candidate != elem) {
return false;
}
}
return true;
}
}
</source>
Clone an array
<source lang="java">
/*
* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * */
/**
* @author Stephen Colebourne * @author Moritz Petersen * @author * @author Maarten Coene * @since 2.0 * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $ */
public class Main {
// Clone //----------------------------------------------------------------------- /** * <p>Shallow clones an array returning a typecast result and handling*
null
.
**
The objects in the array are not cloned, thus there is no special * handling for multi-dimensional arrays.
**
This method returns null
for a null
input array.
* * @param array the array to shallow clone, may be*null
* @return the cloned array,null
ifnull
input */ public static Object[] clone(Object[] array) { if (array == null) { return null; } return (Object[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may be*null
* @return the cloned array,null
ifnull
input */ public static long[] clone(long[] array) { if (array == null) { return null; } return (long[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may be*null
* @return the cloned array,null
ifnull
input */ public static int[] clone(int[] array) { if (array == null) { return null; } return (int[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may be*null
* @return the cloned array,null
ifnull
input */ public static short[] clone(short[] array) { if (array == null) { return null; } return (short[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may be*null
* @return the cloned array,null
ifnull
input */ public static char[] clone(char[] array) { if (array == null) { return null; } return (char[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may be*null
* @return the cloned array,null
ifnull
input */ public static byte[] clone(byte[] array) { if (array == null) { return null; } return (byte[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may be*null
* @return the cloned array,null
ifnull
input */ public static double[] clone(double[] array) { if (array == null) { return null; } return (double[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may be*null
* @return the cloned array,null
ifnull
input */ public static float[] clone(float[] array) { if (array == null) { return null; } return (float[]) array.clone(); } /**
Clones an array returning a typecast result and handling
* null
.
**
This method returns null
for a null
input array.
* * @param array the array to clone, may benull
* @return the cloned array,null
ifnull
input */ public static boolean[] clone(boolean[] array) { if (array == null) { return null; } return (boolean[]) array.clone(); }
}
</source>
Clones a map and prefixes the keys in the clone
<source lang="java">
import java.util.HashMap; import java.util.Iterator; import java.util.Map;
public class Main {
/** * Clones a map and prefixes the keys in the clone, e.g. * for mapping "JAVA_HOME" to "env.JAVA_HOME" to simulate * the behaviour of ANT. * * @param source the source map * @param prefix the prefix used for all names * @return the clone of the source map */ public static Map prefix(Map source, String prefix) { if(source == null) { return null; } Map result = new HashMap(); Iterator iter = source.entrySet().iterator(); while(iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object value = entry.getValue(); result.put(prefix + "." + key.toString(), value); } return result; }
}
</source>
Clones the lhs map and add all things from the rhs map.
<source lang="java">
import java.util.HashMap; import java.util.Map;
public class Main {
/** * Clones the lhs map and add all things from the * rhs map. * * @param lhs the first map * @param rhs the second map * @return the merged map */ public static Map merge(Map lhs, Map rhs) { Map result = null; if((lhs == null) || (lhs.size() == 0)) { result = copy(rhs); } else if((rhs == null) || (rhs.size() == 0)) { result = copy(lhs); } else { result = copy(lhs); result.putAll(rhs); } return result; } /** * Clones a map. * * @param source the source map * @return the clone of the source map */ public static Map copy(Map source) { if(source == null) { return null; } Map result = new HashMap(); result.putAll(source); return result; }
}
</source>
Deep clone collection: Returns a new collection containing clones of all the items in the specified collection.
<source lang="java">
/*
* JCommon : a free general purpose class library for the Java(tm) platform * * * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jcommon/index.html * * This library 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 library 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 library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ------------------- * ShapeUtilities.java * ------------------- * (C)opyright 2003-2008, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: ShapeUtilities.java,v 1.18 2008/06/02 06:58:28 mungady Exp $ * * Changes * ------- * 13-Aug-2003 : Version 1 (DG); * 16-Mar-2004 : Moved rotateShape() from RefineryUtilities.java to here (DG); * 13-May-2004 : Added new shape creation methods (DG); * 30-Sep-2004 : Added createLineRegion() method (DG); * Moved drawRotatedShape() method from RefineryUtilities class * to this class (DG); * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG); * 26-Oct-2004 : Added a method to test the equality of two Line2D * instances (DG); * 10-Nov-2004 : Added new translateShape() and equal(Ellipse2D, Ellipse2D) * methods (DG); * 11-Nov-2004 : Renamed translateShape() --> createTranslatedShape() (DG); * 07-Jan-2005 : Minor Javadoc fix (DG); * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG); * 21-Jan-2005 : Modified return type of RectangleAnchor.coordinates() * method (DG); * 22-Feb-2005 : Added equality tests for Arc2D and GeneralPath (DG); * 16-Mar-2005 : Fixed bug where equal(Shape, Shape) fails for two Polygon * instances (DG); * 01-Jun-2008 : Fixed bug in equal(GeneralPath, GeneralPath) method (DG); * */
import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Iterator; /**
* Utility methods for {@link Shape} objects. * * @author David Gilbert */
public class Main {
/** * Returns a new collection containing clones of all the items in the * specified collection. * * @param collection * the collection (null
not permitted). * @return A new collection containing clones of all the items in the * specified collection. * @throws CloneNotSupportedException * if any of the items in the collection cannot be cloned. */ public static Collection deepClone(final Collection collection) throws CloneNotSupportedException { if (collection == null) { throw new IllegalArgumentException("Null "collection" argument."); } // all JDK-Collections are cloneable ... // and if the collection is not clonable, then we should throw // a CloneNotSupportedException anyway ... final Collection result = (Collection) clone(collection); result.clear(); final Iterator iterator = collection.iterator(); while (iterator.hasNext()) { final Object item = iterator.next(); if (item != null) { result.add(clone(item)); } else { result.add(null); } } return result; } /** * Returns a clone of the specified object, if it can be cloned, otherwise * throws a CloneNotSupportedException. * * @param object * the object to clone (null
not permitted). * @return A clone of the specified object. * @throws CloneNotSupportedException * if the object cannot be cloned. */ public static Object clone(final Object object) throws CloneNotSupportedException { if (object == null) { throw new IllegalArgumentException("Null "object" argument."); } try { final Method method = object.getClass().getMethod("clone", (Class[]) null); if (Modifier.isPublic(method.getModifiers())) { return method.invoke(object, (Object[]) null); } } catch (NoSuchMethodException e) { System.out.println("Object without clone() method is impossible."); } catch (IllegalAccessException e) { System.out.println("Object.clone(): unable to call method."); } catch (InvocationTargetException e) { System.out.println("Object without clone() method is impossible."); } throw new CloneNotSupportedException("Failed to clone."); }
}
</source>
Disjoint two collections
<source lang="java">
/*
* Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */
import java.util.Collection; import java.util.Iterator; public final class CollectionsUtil {
public static boolean disjoint(Collection c1, Collection c2) { if (c1.size() > c2.size()) { Collection c = c1; c1 = c2; c2 = c; } for (Iterator iterator = c2.iterator(); iterator.hasNext();) { Object o = (Object)iterator.next(); if (c1.contains(o)) { return false; } } return true; }
}
</source>
Find a value of the given type in the given Collection
<source lang="java">
import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; /*
* Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/**
* Miscellaneous collection utility methods. * Mainly for internal use within the framework. * * @author Juergen Hoeller * @author Rob Harrop * @since 1.1.3 */
abstract class CollectionUtils {
/** * Find a value of the given type in the given Collection. * @param collection the Collection to search * @param type the type to look for * @return a value of the given type found, ornull
if none * @throws IllegalArgumentException if more than one value of the given type found */ public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException { if (isEmpty(collection)) { return null; } Class typeToUse = (type != null ? type : Object.class); Object value = null; for (Iterator it = collection.iterator(); it.hasNext();) { Object obj = it.next(); if (typeToUse.isInstance(obj)) { if (value != null) { throw new IllegalArgumentException("More than one value of type [" + typeToUse.getName() + "] found"); } value = obj; } } return value; } /** * Returntrue
if the supplied Collection isnull
* or empty. Otherwise, returnfalse
. * @param collection the Collection to check * @return whether the given Collection is empty */ public static boolean isEmpty(Collection collection) { return (collection == null || collection.isEmpty()); } /** * Returntrue
if the supplied Map isnull
* or empty. Otherwise, returnfalse
. * @param map the Map to check * @return whether the given Map is empty */ public static boolean isEmpty(Map map) { return (map == null || map.isEmpty()); }
/** * Return the first element in "candidates
" that is contained in * "source
". If no element in "candidates
" is present in * "source
" returnsnull
. Iteration order is * {@link Collection} implementation specific. * @param source the source Collection * @param candidates the candidates to search for * @return the first present object, ornull
if not found */ public static Object findFirstMatch(Collection source, Collection candidates) { if (isEmpty(source) || isEmpty(candidates)) { return null; } for (Iterator it = candidates.iterator(); it.hasNext();) { Object candidate = it.next(); if (source.contains(candidate)) { return candidate; } } return null; }
/** * Determine whether the given Collection only contains a single unique object. * @param collection the Collection to check * @returntrue
if the collection contains a single reference or * multiple references to the same instance,false
else */ public static boolean hasUniqueObject(Collection collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Iterator it = collection.iterator(); it.hasNext();) { Object elem = it.next(); if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
}
</source>
Get the difference of two collections
<source lang="java">
import java.util.ArrayList; import java.util.Collection;
public class Utils {
public static <T> Collection<T> diff(Collection<T> c1, Collection<T> c2) { if (c1 == null || c1.size() == 0 || c2 == null || c2.size() == 0) { return c1; } Collection<T> difference = new ArrayList<T>(); for (T item : c1) { if (!c2.contains(item)) { difference.add(item); } } return difference;
}
}
</source>
Is collection is empty, and all of its elements are empty
<source lang="java">
import java.util.Collection; import java.util.Iterator;
public class Utils {
public static <T> boolean isEmpty(Collection<T> c) { if (c == null || c.size() == 0) { return true; } for (Iterator<T> iter = c.iterator(); iter.hasNext();) { if (iter.next() != null) { return false; } } return true; }
}
</source>
Return the first element in "candidates" that is contained in source
<source lang="java">
import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; /*
* Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/**
* Miscellaneous collection utility methods. * Mainly for internal use within the framework. * * @author Juergen Hoeller * @author Rob Harrop * @since 1.1.3 */
abstract class CollectionUtils {
/** * Return the first element in "candidates
" that is contained in * "source
". If no element in "candidates
" is present in * "source
" returnsnull
. Iteration order is * {@link Collection} implementation specific. * @param source the source Collection * @param candidates the candidates to search for * @return the first present object, ornull
if not found */ public static Object findFirstMatch(Collection source, Collection candidates) { if (isEmpty(source) || isEmpty(candidates)) { return null; } for (Iterator it = candidates.iterator(); it.hasNext();) { Object candidate = it.next(); if (source.contains(candidate)) { return candidate; } } return null; } /** * Returntrue
if the supplied Collection isnull
* or empty. Otherwise, returnfalse
. * @param collection the Collection to check * @return whether the given Collection is empty */ public static boolean isEmpty(Collection collection) { return (collection == null || collection.isEmpty()); } /** * Returntrue
if the supplied Map isnull
* or empty. Otherwise, returnfalse
. * @param map the Map to check * @return whether the given Map is empty */ public static boolean isEmpty(Map map) { return (map == null || map.isEmpty()); }
}
</source>
Return true if any element in "candidates" is contained in source
<source lang="java">
import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; /*
* Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/**
* Miscellaneous collection utility methods. * Mainly for internal use within the framework. * * @author Juergen Hoeller * @author Rob Harrop * @since 1.1.3 */
abstract class CollectionUtils {
/** * Returntrue
if any element in "candidates
" is * contained in "source
"; otherwise returnsfalse
. * @param source the source Collection * @param candidates the candidates to search for * @return whether any of the candidates has been found */ public static boolean containsAny(Collection source, Collection candidates) { if (isEmpty(source) || isEmpty(candidates)) { return false; } for (Iterator it = candidates.iterator(); it.hasNext();) { if (source.contains(it.next())) { return true; } } return false; } /** * Returntrue
if the supplied Collection isnull
* or empty. Otherwise, returnfalse
. * @param collection the Collection to check * @return whether the given Collection is empty */ public static boolean isEmpty(Collection collection) { return (collection == null || collection.isEmpty()); } /** * Returntrue
if the supplied Map isnull
* or empty. Otherwise, returnfalse
. * @param map the Map to check * @return whether the given Map is empty */ public static boolean isEmpty(Map map) { return (map == null || map.isEmpty()); }
/** * Return the first element in "candidates
" that is contained in * "source
". If no element in "candidates
" is present in * "source
" returnsnull
. Iteration order is * {@link Collection} implementation specific. * @param source the source Collection * @param candidates the candidates to search for * @return the first present object, ornull
if not found */ public static Object findFirstMatch(Collection source, Collection candidates) { if (isEmpty(source) || isEmpty(candidates)) { return null; } for (Iterator it = candidates.iterator(); it.hasNext();) { Object candidate = it.next(); if (source.contains(candidate)) { return candidate; } } return null; } /** * Find a value of the given type in the given Collection. * @param collection the Collection to search * @param type the type to look for * @return a value of the given type found, ornull
if none * @throws IllegalArgumentException if more than one value of the given type found */ public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException { if (isEmpty(collection)) { return null; } Class typeToUse = (type != null ? type : Object.class); Object value = null; for (Iterator it = collection.iterator(); it.hasNext();) { Object obj = it.next(); if (typeToUse.isInstance(obj)) { if (value != null) { throw new IllegalArgumentException("More than one value of type [" + typeToUse.getName() + "] found"); } value = obj; } } return value; } /** * Determine whether the given Collection only contains a single unique object. * @param collection the Collection to check * @returntrue
if the collection contains a single reference or * multiple references to the same instance,false
else */ public static boolean hasUniqueObject(Collection collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Iterator it = collection.iterator(); it.hasNext();) { Object elem = it.next(); if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
}
</source>
Simple implementation of a generic Bag
<source lang="java">
/*
* FindBugs - Find Bugs in Java programs * Copyright (C) 2003-2007 University of Maryland * * This library 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 library 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 library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; /**
* Simple implementation of a Bag * * @author pugh */
public class Bag<E> {
final Map<E,Integer> map; public Bag() { map = new HashMap<E, Integer>(); } public Bag(Map<E, Integer> map) { this.map = map; } public boolean add(E e) { Integer v = map.get(e); if (v == null) map.put(e, 1); else map.put(e, v+1); return true; } public boolean add(E e, int count) { Integer v = map.get(e); if (v == null) map.put(e, count); else map.put(e, v+count); return true; } public Set<E> keySet() { return map.keySet(); } public Collection<Map.Entry<E, Integer>> entrySet() { return map.entrySet(); } public int getCount(E e) { Integer v = map.get(e); if (v == null) return 0; else return v; }
}
</source>
<source lang="java">
/* CollectionsX.java {{IS_NOTE
Purpose: Description: History: 2001/09/15 13:46:20, Create, Tom M. Yeh.
}}IS_NOTE Copyright (C) 2001 Potix Corporation. All Rights Reserved. {{IS_RIGHT
This program is distributed under GPL Version 3.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
- /
import java.util.AbstractCollection; import java.util.Collection; import java.util.Collections; import java.util.Set; import java.util.Map; import java.util.AbstractList; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.ListIterator; import java.util.Enumeration; import java.util.NoSuchElementException;
/**
* The collection related utilities. * * @author tomyeh * @see java.util.Collections */
public class CollectionsX {
/** An enumeration on top of a collection or iterator. */ public static final class CollectionEnumeration implements Enumeration { private final Iterator _iter; public CollectionEnumeration(Collection c) { this(c != null ? c.iterator(): null); } public CollectionEnumeration(Iterator iter) { _iter = iter; } public final boolean hasMoreElements() { return _iter != null && _iter.hasNext(); } public final Object nextElement() { if (_iter != null) return _iter.next(); throw new NoSuchElementException(); } } /** An enumeration on top of an array. */ public static final class ArrayEnumeration implements Enumeration { private final Object[] _ary; private int _cursor = 0; public ArrayEnumeration(Object[] ary) { _ary = ary; } public final boolean hasMoreElements() { return _ary != null && _cursor < _ary.length; } public final Object nextElement() { if (hasMoreElements()) return _ary[_cursor++]; throw new NoSuchElementException(); } } /** An enumeration that enumerates one element. */ public static final class OneEnumeration implements Enumeration { private boolean _nomore; private final Object _one; public OneEnumeration(Object one) { _one = one; } public final boolean hasMoreElements() { return !_nomore; } public final Object nextElement() { if (_nomore) throw new NoSuchElementException(); _nomore = true; return _one; } } /** An readonly collection on top of an array. */ public static final class ArrayCollection extends AbstractCollection { private final Object[] _ary; public ArrayCollection(Object[] ary) { _ary = ary; } public final int size() { return _ary != null ? _ary.length: 0; } public Iterator iterator() { return new ArrayIterator(_ary); } } /** An readonly list on top of an array. */ public static final class ArrayList extends AbstractList { private final Object[] _ary; public ArrayList(Object[] ary) { _ary = ary; } public final int size() { return _ary != null ? _ary.length: 0; } public final Object get(int index) { return _ary[index]; } } /** An iterator on top of an array. */ public static class ArrayIterator implements Iterator { /*package*/ final Object[] _ary; /*package*/ int _cursor = 0, _last = -1; /** @param ary an array or null. */ public ArrayIterator(Object[] ary) { _ary = ary; } public final boolean hasNext() { return _ary != null && _cursor < _ary.length; } public final Object next() { if (hasNext()) return _ary[_last = _cursor++]; throw new NoSuchElementException("cursor="+_cursor); } public final void remove() { throw new UnsupportedOperationException(); } } public static class ArrayListIterator extends ArrayIterator implements ListIterator { /** @param ary an array or null. */ public ArrayListIterator(Object[] ary) { super(ary); } /** @param ary an array or null. */ public ArrayListIterator(Object[] ary, int index) { super(ary); _cursor = index; final int len = _ary != null ? _ary.length: 0; if (_cursor < 0 || _cursor > len) throw new IndexOutOfBoundsException("index="+index+" but len="+len); } public final boolean hasPrevious() { return _ary != null && _cursor > 0; } public final Object previous() { if (hasPrevious()) return _ary[_last = --_cursor]; throw new NoSuchElementException("cursor="+_cursor); } public final int nextIndex() { return _cursor; } public final int previousIndex() { return _cursor - 1; } public final void set(Object o) { if (_last < 0) throw new IllegalStateException("neither next nor previous have been called"); _ary[_last] = o; } public final void add(Object o) { throw new UnsupportedOperationException(); } } /** A collection that contains only one element. */ public static final class OneCollection extends AbstractCollection { private final Object _one; public OneCollection(Object one) { _one = one; } public final int size() { return 1; } public Iterator iterator() { return new OneIterator(_one); } } /** An iterator that iterates one element. */ public static final class OneIterator implements Iterator { private boolean _nomore; private final Object _one; public OneIterator(Object one) { _one = one; } public final boolean hasNext() { return !_nomore; } public final Object next() { if (_nomore) throw new NoSuchElementException(); _nomore = true; return _one; } public final void remove() { throw new UnsupportedOperationException(); } } /** An iterator that iterates thru an Enumeration. */ public static final class EnumerationIterator implements Iterator { private final Enumeration _enm; /** * @param enm the enumeration. If null, it means empty. */ public EnumerationIterator(Enumeration enm) { _enm = enm; } public final boolean hasNext() { return _enm != null && _enm.hasMoreElements(); } public final Object next() { if (_enm == null) throw new NoSuchElementException(); return _enm.nextElement(); } public final void remove() { throw new UnsupportedOperationException(); } }; /** Empty iterator. */ public static final Iterator EMPTY_ITERATOR = new Iterator() { public final boolean hasNext() { return false; } public final Object next() { throw new NoSuchElementException(); } public final void remove() { throw new IllegalStateException(); } }; /** Empty enumeration. */ public static final Enumeration EMPTY_ENUMERATION = new Enumeration() { public final boolean hasMoreElements() { return false; } public final Object nextElement() { throw new NoSuchElementException(); } }; /** * Returns the specified range of the specified collection into a new array. * The initial index of the range (from) must lie between zero * and col.size, inclusive. * The final index of the range (to), which must be greater than or * equal to from.*
The returned array will be "safe" in that no references to it are * maintained by this list. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * @param col the collection to be copied. * @param from the initial index of the range to be copied, inclusive. * @param to the final index of the range to be copied, exclusive. * @since 3.0.6 */ public static final Object[] toArray(Collection col, int from, int to) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); Object[] result = new Object[newLength]; int i = 0, j = 0; for (Iterator it = col.iterator(); it.hasNext() && i < result.length;) { if (j++ < from) { it.next(); continue; } result[i++] = it.next(); } return result; } /** Adds all elements returned by the iterator to a collection. * @param iter the iterator; null is OK * @return the number element being added */ public static final int addAll(Collection col, Iterator iter) { int cnt = 0; if (iter != null) for (; iter.hasNext(); ++cnt) col.add(iter.next()); return cnt; } /** Adds all elements returned by the enumerator to a collection. * @param enm the enumeration; null is OK * @return the number element being added */ public static final int addAll(Collection col, Enumeration enm) { int cnt = 0; if (enm != null) for (; enm.hasMoreElements(); ++cnt) col.add(enm.nextElement()); return cnt; } /** Adds all elements of an array to a collection. * @param ary the array; null is OK * @return the number element being added */ public static final int addAll(Collection col, Object[] ary) { int cnt = 0; if (ary != null) for (; cnt < ary.length; ++cnt) col.add(ary[cnt]); return cnt; } /** Tests whether two sets has any intersection. */ public static final boolean isIntersected(Set a, Set b) { final int sza = a != null ? a.size(): 0; final int szb = b != null ? b.size(): 0; if (sza == 0 || szb == 0) return false; final Set large, small; if (sza > szb) { large = a; small = b; } else { large = b; small = a; } for (final Iterator it = small.iterator(); it.hasNext();) if (large.contains(it.next())) return true; return false; } /** * Based on the given collection type of Object, return an iterator. The * Collection type of object can be Collection, Map (return the entry), or * Array. */ public static final Iterator iterator(Object obj) { if (obj instanceof Object[]) { return new ArrayIterator((Object[])obj); } else if (obj instanceof Collection) { return ((Collection)obj).iterator(); } else if (obj instanceof Map) { return ((Map)obj).entrySet().iterator(); } throw new IllegalArgumentException("obj must be a Collection, a Map, or an Object array. obj: "+obj); } } </source>
Whether Collection only contains a single unique object
<source lang="java">
import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; /*
* Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/**
* Miscellaneous collection utility methods. * Mainly for internal use within the framework. * * @author Juergen Hoeller * @author Rob Harrop * @since 1.1.3 */
abstract class CollectionUtils {
/** * Determine whether the given Collection only contains a single unique object. * @param collection the Collection to check * @returntrue
if the collection contains a single reference or * multiple references to the same instance,false
else */ public static boolean hasUniqueObject(Collection collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Iterator it = collection.iterator(); it.hasNext();) { Object elem = it.next(); if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; } /** * Returntrue
if the supplied Collection isnull
* or empty. Otherwise, returnfalse
. * @param collection the Collection to check * @return whether the given Collection is empty */ public static boolean isEmpty(Collection collection) { return (collection == null || collection.isEmpty()); } /** * Returntrue
if the supplied Map isnull
* or empty. Otherwise, returnfalse
. * @param map the Map to check * @return whether the given Map is empty */ public static boolean isEmpty(Map map) { return (map == null || map.isEmpty()); }
/** * Return the first element in "candidates
" that is contained in * "source
". If no element in "candidates
" is present in * "source
" returnsnull
. Iteration order is * {@link Collection} implementation specific. * @param source the source Collection * @param candidates the candidates to search for * @return the first present object, ornull
if not found */ public static Object findFirstMatch(Collection source, Collection candidates) { if (isEmpty(source) || isEmpty(candidates)) { return null; } for (Iterator it = candidates.iterator(); it.hasNext();) { Object candidate = it.next(); if (source.contains(candidate)) { return candidate; } } return null; }
}
</source>