📄 listutils.java
字号:
public static int hashCodeForList(final Collection list) {
if (list == null) {
return 0;
}
int hashCode = 1;
Iterator it = list.iterator();
Object obj = null;
while (it.hasNext()) {
obj = it.next();
hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
}
return hashCode;
}
//-----------------------------------------------------------------------
/**
* Returns a List containing all the elements in <code>collection</code>
* that are also in <code>retain</code>. The cardinality of an element <code>e</code>
* in the returned list is the same as the cardinality of <code>e</code>
* in <code>collection</code> unless <code>retain</code> does not contain <code>e</code>, in which
* case the cardinality is zero. This method is useful if you do not wish to modify
* the collection <code>c</code> and thus cannot call <code>collection.retainAll(retain);</code>.
*
* @param collection the collection whose contents are the target of the #retailAll operation
* @param retain the collection containing the elements to be retained in the returned collection
* @return a <code>List</code> containing all the elements of <code>c</code>
* that occur at least once in <code>retain</code>.
* @throws NullPointerException if either parameter is null
* @since Commons Collections 3.2
*/
public static List retainAll(Collection collection, Collection retain) {
List list = new ArrayList(Math.min(collection.size(), retain.size()));
for (Iterator iter = collection.iterator(); iter.hasNext();) {
Object obj = iter.next();
if (retain.contains(obj)) {
list.add(obj);
}
}
return list;
}
/**
* Removes the elements in <code>remove</code> from <code>collection</code>. That is, this
* method returns a list containing all the elements in <code>c</code>
* that are not in <code>remove</code>. The cardinality of an element <code>e</code>
* in the returned collection is the same as the cardinality of <code>e</code>
* in <code>collection</code> unless <code>remove</code> contains <code>e</code>, in which
* case the cardinality is zero. This method is useful if you do not wish to modify
* <code>collection</code> and thus cannot call <code>collection.removeAll(remove);</code>.
*
* @param collection the collection from which items are removed (in the returned collection)
* @param remove the items to be removed from the returned <code>collection</code>
* @return a <code>List</code> containing all the elements of <code>c</code> except
* any elements that also occur in <code>remove</code>.
* @throws NullPointerException if either parameter is null
* @since Commons Collections 3.2
*/
public static List removeAll(Collection collection, Collection remove) {
List list = new ArrayList();
for (Iterator iter = collection.iterator(); iter.hasNext();) {
Object obj = iter.next();
if (remove.contains(obj) == false) {
list.add(obj);
}
}
return list;
}
//-----------------------------------------------------------------------
/**
* Returns a synchronized list backed by the given list.
* <p>
* You must manually synchronize on the returned buffer's iterator to
* avoid non-deterministic behavior:
*
* <pre>
* List list = ListUtils.synchronizedList(myList);
* synchronized (list) {
* Iterator i = list.iterator();
* while (i.hasNext()) {
* process (i.next());
* }
* }
* </pre>
*
* This method uses the implementation in the decorators subpackage.
*
* @param list the list to synchronize, must not be null
* @return a synchronized list backed by the given list
* @throws IllegalArgumentException if the list is null
*/
public static List synchronizedList(List list) {
return SynchronizedList.decorate(list);
}
/**
* Returns an unmodifiable list backed by the given list.
* <p>
* This method uses the implementation in the decorators subpackage.
*
* @param list the list to make unmodifiable, must not be null
* @return an unmodifiable list backed by the given list
* @throws IllegalArgumentException if the list is null
*/
public static List unmodifiableList(List list) {
return UnmodifiableList.decorate(list);
}
/**
* Returns a predicated (validating) list backed by the given list.
* <p>
* Only objects that pass the test in the given predicate can be added to the list.
* Trying to add an invalid object results in an IllegalArgumentException.
* It is important not to use the original list after invoking this method,
* as it is a backdoor for adding invalid objects.
*
* @param list the list to predicate, must not be null
* @param predicate the predicate for the list, must not be null
* @return a predicated list backed by the given list
* @throws IllegalArgumentException if the List or Predicate is null
*/
public static List predicatedList(List list, Predicate predicate) {
return PredicatedList.decorate(list, predicate);
}
/**
* Returns a typed list backed by the given list.
* <p>
* Only objects of the specified type can be added to the list.
*
* @param list the list to limit to a specific type, must not be null
* @param type the type of objects which may be added to the list
* @return a typed list backed by the specified list
*/
public static List typedList(List list, Class type) {
return TypedList.decorate(list, type);
}
/**
* Returns a transformed list backed by the given list.
* <p>
* Each object is passed through the transformer as it is added to the
* List. It is important not to use the original list after invoking this
* method, as it is a backdoor for adding untransformed objects.
*
* @param list the list to predicate, must not be null
* @param transformer the transformer for the list, must not be null
* @return a transformed list backed by the given list
* @throws IllegalArgumentException if the List or Transformer is null
*/
public static List transformedList(List list, Transformer transformer) {
return TransformedList.decorate(list, transformer);
}
/**
* Returns a "lazy" list whose elements will be created on demand.
* <p>
* When the index passed to the returned list's {@link List#get(int) get}
* method is greater than the list's size, then the factory will be used
* to create a new object and that object will be inserted at that index.
* <p>
* For instance:
*
* <pre>
* Factory factory = new Factory() {
* public Object create() {
* return new Date();
* }
* }
* List lazy = ListUtils.lazyList(new ArrayList(), factory);
* Object obj = lazy.get(3);
* </pre>
*
* After the above code is executed, <code>obj</code> will contain
* a new <code>Date</code> instance. Furthermore, that <code>Date</code>
* instance is the fourth element in the list. The first, second,
* and third element are all set to <code>null</code>.
*
* @param list the list to make lazy, must not be null
* @param factory the factory for creating new objects, must not be null
* @return a lazy list backed by the given list
* @throws IllegalArgumentException if the List or Factory is null
*/
public static List lazyList(List list, Factory factory) {
return LazyList.decorate(list, factory);
}
/**
* Returns a fixed-sized list backed by the given list.
* Elements may not be added or removed from the returned list, but
* existing elements can be changed (for instance, via the
* {@link List#set(int,Object)} method).
*
* @param list the list whose size to fix, must not be null
* @return a fixed-size list backed by that list
* @throws IllegalArgumentException if the List is null
*/
public static List fixedSizeList(List list) {
return FixedSizeList.decorate(list);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -