📄 listutils.java
字号:
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* 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.
*/
package org.apache.commons.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.list.FixedSizeList;
import org.apache.commons.collections.list.LazyList;
import org.apache.commons.collections.list.PredicatedList;
import org.apache.commons.collections.list.SynchronizedList;
import org.apache.commons.collections.list.TransformedList;
import org.apache.commons.collections.list.TypedList;
import org.apache.commons.collections.list.UnmodifiableList;
/**
* Provides utility methods and decorators for {@link List} instances.
*
* @since Commons Collections 1.0
* @version $Revision: 348013 $ $Date: 2005-11-21 23:24:45 +0000 (Mon, 21 Nov 2005) $
*
* @author Federico Barbieri
* @author Peter Donald
* @author Paul Jack
* @author Stephen Colebourne
* @author Neil O'Toole
* @author Matthew Hawthorne
*/
public class ListUtils {
/**
* An empty unmodifiable list.
* This uses the {@link Collections Collections} implementation
* and is provided for completeness.
*/
public static final List EMPTY_LIST = Collections.EMPTY_LIST;
/**
* <code>ListUtils</code> should not normally be instantiated.
*/
public ListUtils() {
}
//-----------------------------------------------------------------------
/**
* Returns a new list containing all elements that are contained in
* both given lists.
*
* @param list1 the first list
* @param list2 the second list
* @return the intersection of those two lists
* @throws NullPointerException if either list is null
*/
public static List intersection(final List list1, final List list2) {
final ArrayList result = new ArrayList();
final Iterator iterator = list2.iterator();
while (iterator.hasNext()) {
final Object o = iterator.next();
if (list1.contains(o)) {
result.add(o);
}
}
return result;
}
/**
* Subtracts all elements in the second list from the first list,
* placing the results in a new list.
* <p>
* This differs from {@link List#removeAll(Collection)} in that
* cardinality is respected; if <Code>list1</Code> contains two
* occurrences of <Code>null</Code> and <Code>list2</Code> only
* contains one occurrence, then the returned list will still contain
* one occurrence.
*
* @param list1 the list to subtract from
* @param list2 the list to subtract
* @return a new list containing the results
* @throws NullPointerException if either list is null
*/
public static List subtract(final List list1, final List list2) {
final ArrayList result = new ArrayList(list1);
final Iterator iterator = list2.iterator();
while (iterator.hasNext()) {
result.remove(iterator.next());
}
return result;
}
/**
* Returns the sum of the given lists. This is their intersection
* subtracted from their union.
*
* @param list1 the first list
* @param list2 the second list
* @return a new list containing the sum of those lists
* @throws NullPointerException if either list is null
*/
public static List sum(final List list1, final List list2) {
return subtract(union(list1, list2), intersection(list1, list2));
}
/**
* Returns a new list containing the second list appended to the
* first list. The {@link List#addAll(Collection)} operation is
* used to append the two given lists into a new list.
*
* @param list1 the first list
* @param list2 the second list
* @return a new list containing the union of those lists
* @throws NullPointerException if either list is null
*/
public static List union(final List list1, final List list2) {
final ArrayList result = new ArrayList(list1);
result.addAll(list2);
return result;
}
/**
* Tests two lists for value-equality as per the equality contract in
* {@link java.util.List#equals(java.lang.Object)}.
* <p>
* This method is useful for implementing <code>List</code> when you cannot
* extend AbstractList. The method takes Collection instances to enable other
* collection types to use the List implementation algorithm.
* <p>
* The relevant text (slightly paraphrased as this is a static method) is:
* <blockquote>
* Compares the two list objects for equality. Returns
* <tt>true</tt> if and only if both
* lists have the same size, and all corresponding pairs of elements in
* the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and
* <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null :
* e1.equals(e2))</tt>.) In other words, two lists are defined to be
* equal if they contain the same elements in the same order. This
* definition ensures that the equals method works properly across
* different implementations of the <tt>List</tt> interface.
* </blockquote>
*
* <b>Note:</b> The behaviour of this method is undefined if the lists are
* modified during the equals comparison.
*
* @see java.util.List
* @param list1 the first list, may be null
* @param list2 the second list, may be null
* @return whether the lists are equal by value comparison
*/
public static boolean isEqualList(final Collection list1, final Collection list2) {
if (list1 == list2) {
return true;
}
if (list1 == null || list2 == null || list1.size() != list2.size()) {
return false;
}
Iterator it1 = list1.iterator();
Iterator it2 = list2.iterator();
Object obj1 = null;
Object obj2 = null;
while (it1.hasNext() && it2.hasNext()) {
obj1 = it1.next();
obj2 = it2.next();
if (!(obj1 == null ? obj2 == null : obj1.equals(obj2))) {
return false;
}
}
return !(it1.hasNext() || it2.hasNext());
}
/**
* Generates a hash code using the algorithm specified in
* {@link java.util.List#hashCode()}.
* <p>
* This method is useful for implementing <code>List</code> when you cannot
* extend AbstractList. The method takes Collection instances to enable other
* collection types to use the List implementation algorithm.
*
* @see java.util.List#hashCode()
* @param list the list to generate the hashCode for, may be null
* @return the hash code
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -