<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FCollections%2FCollections_Search</id>
		<title>Java Tutorial/Collections/Collections Search - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FCollections%2FCollections_Search"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Collections/Collections_Search&amp;action=history"/>
		<updated>2026-05-14T05:59:07Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Collections/Collections_Search&amp;diff=4666&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Collections/Collections_Search&amp;diff=4666&amp;oldid=prev"/>
				<updated>2010-06-01T05:04:10Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:04, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Collections/Collections_Search&amp;diff=4665&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Collections/Collections_Search&amp;diff=4665&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  A binary search implementation. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.&lt;br /&gt;
 * &lt;br /&gt;
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * The contents of this file are subject to the terms of either the GNU&lt;br /&gt;
 * General Public License Version 2 only (&amp;quot;GPL&amp;quot;) or the Common Development&lt;br /&gt;
 * and Distribution License(&amp;quot;CDDL&amp;quot;) (collectively, the &amp;quot;License&amp;quot;).  You&lt;br /&gt;
 * may not use this file except in compliance with the License. You can obtain&lt;br /&gt;
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html&lt;br /&gt;
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific&lt;br /&gt;
 * language governing permissions and limitations under the License.&lt;br /&gt;
 * &lt;br /&gt;
 * When distributing the software, include this License Header Notice in each&lt;br /&gt;
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.&lt;br /&gt;
 * Sun designates this particular file as subject to the &amp;quot;Classpath&amp;quot; exception&lt;br /&gt;
 * as provided by Sun in the GPL Version 2 section of the License file that&lt;br /&gt;
 * accompanied this code.  If applicable, add the following below the License&lt;br /&gt;
 * Header, with the fields enclosed by brackets [] replaced by your own&lt;br /&gt;
 * identifying information: &amp;quot;Portions Copyrighted [year]&lt;br /&gt;
 * [name of copyright owner]&amp;quot;&lt;br /&gt;
 * &lt;br /&gt;
 * Contributor(s):&lt;br /&gt;
 * &lt;br /&gt;
 * If you wish your version of this file to be governed by only the CDDL or&lt;br /&gt;
 * only the GPL Version 2, indicate your decision by adding &amp;quot;[Contributor]&lt;br /&gt;
 * elects to include this software in this distribution under the [CDDL or GPL&lt;br /&gt;
 * Version 2] license.&amp;quot;  If you don&amp;quot;t indicate a single choice of license, a&lt;br /&gt;
 * recipient has the option to distribute your version of this file under&lt;br /&gt;
 * either the CDDL, the GPL Version 2 or to extend the choice of license to&lt;br /&gt;
 * its licensees as provided above.  However, if you add GPL Version 2 code&lt;br /&gt;
 * and therefore, elected the GPL Version 2 license, then the option applies&lt;br /&gt;
 * only if the new code is made subject to such option by the copyright&lt;br /&gt;
 * holder.&lt;br /&gt;
 */&lt;br /&gt;
// ----------------------------------------------------------------------------&lt;br /&gt;
/**&lt;br /&gt;
 * A binary search implementation.&lt;br /&gt;
 */&lt;br /&gt;
public class BinarySearch {&lt;br /&gt;
  public int find(final int[] data, final int key) {&lt;br /&gt;
    int low = 0, high = data.length - 1;&lt;br /&gt;
    while (low &amp;lt;= high) {&lt;br /&gt;
      final int i = (low + high) &amp;gt;&amp;gt; 1;&lt;br /&gt;
      final int v = data[i];&lt;br /&gt;
      if (v == key)&lt;br /&gt;
        return i; // this line does not get covered unless there is a match&lt;br /&gt;
      else if (v &amp;lt; key)&lt;br /&gt;
        low = i + 1;&lt;br /&gt;
      else&lt;br /&gt;
        // v &amp;gt; key&lt;br /&gt;
        high = i - 1;&lt;br /&gt;
    }&lt;br /&gt;
    return -1;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Binary Searching ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    String str[] = { &amp;quot;B&amp;quot;, &amp;quot;H&amp;quot;, &amp;quot;L&amp;quot;, &amp;quot;M&amp;quot;, &amp;quot;I&amp;quot;, &amp;quot;N&amp;quot;, &amp;quot;R&amp;quot; };&lt;br /&gt;
    // Convert to list&lt;br /&gt;
    List list = new ArrayList(Arrays.asList(str));&lt;br /&gt;
    // Ensure list sorted&lt;br /&gt;
    Collections.sort(list);&lt;br /&gt;
    System.out.println(&amp;quot;Sorted list: [length: &amp;quot; + list.size() + &amp;quot;]&amp;quot;);&lt;br /&gt;
    System.out.println(list);&lt;br /&gt;
    // Search for element in list&lt;br /&gt;
    int index = Collections.binarySearch(list, &amp;quot;M&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;Found M @ &amp;quot; + index);&lt;br /&gt;
    // Search for element not in list&lt;br /&gt;
    index = Collections.binarySearch(list, &amp;quot;J&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;Didn&amp;quot;t find J @ &amp;quot; + index);&lt;br /&gt;
    // Insert&lt;br /&gt;
    int newIndex = -index - 1;&lt;br /&gt;
    list.add(newIndex, &amp;quot;J&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;With J added: [length: &amp;quot; + list.size() + &amp;quot;]&amp;quot;);&lt;br /&gt;
    System.out.println(list);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Sorted list: [length: 7]&lt;br /&gt;
[B, H, I, L, M, N, R]&lt;br /&gt;
Found M @ 4&lt;br /&gt;
Didn&amp;quot;t find J @ -4&lt;br /&gt;
With J added: [length: 8]&lt;br /&gt;
[B, H, I, J, L, M, N, R]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Binary search routines ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1998-2002 Carnegie Mellon University.  All rights&lt;br /&gt;
 * reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 * 1. Redistributions of source code must retain the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 * 2. Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer in&lt;br /&gt;
 *    the documentation and/or other materials provided with the&lt;br /&gt;
 *    distribution.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS&amp;quot;&amp;quot; AND&lt;br /&gt;
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY&lt;br /&gt;
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,&lt;br /&gt;
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,&lt;br /&gt;
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY&lt;br /&gt;
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT&lt;br /&gt;
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE&lt;br /&gt;
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Binary search routines.&lt;br /&gt;
 */&lt;br /&gt;
public abstract class BinarySearch {&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Search a sorted array of integers.&lt;br /&gt;
     * @param array Array of integers&lt;br /&gt;
     * @param offset Starting offset of subarray to search&lt;br /&gt;
     * @param length Length of subarray to search&lt;br /&gt;
     * @param x Value to search for&lt;br /&gt;
     * @return largest index i in subarray (offset &amp;amp;lt;= i &amp;amp;lt;= offset+length)&lt;br /&gt;
     * such that all elements below i in the subarray are strictly less&lt;br /&gt;
     * than x.  If x is found in the subarray, then array[i] == x (and i is&lt;br /&gt;
     * the first occurence of x in the subarray).  If x is not found, &lt;br /&gt;
     * then array[i] is where x should be inserted in the sort order.&lt;br /&gt;
     */&lt;br /&gt;
    public static int search (int[] array, int offset, int length, int x) {&lt;br /&gt;
        // handle 0-length subarray case right away&lt;br /&gt;
        if (length &amp;lt;= 0)&lt;br /&gt;
            return offset;&lt;br /&gt;
        int low = offset;&lt;br /&gt;
        int high = offset+length-1;&lt;br /&gt;
        // since length &amp;gt; 0, array[low] and array[high] are valid indices&lt;br /&gt;
        if (x &amp;lt;= array[low])&lt;br /&gt;
            return low;&lt;br /&gt;
        if (x &amp;gt; array[high])&lt;br /&gt;
            return high+1;&lt;br /&gt;
        &lt;br /&gt;
        while (low+1 &amp;lt; high) {&lt;br /&gt;
            // loop invariant: array[low] &amp;lt; x &amp;lt;= array[high],&lt;br /&gt;
            //                 offset &amp;lt;= low &amp;lt; high &amp;lt; offset+length&lt;br /&gt;
            int mid = (low + high)/2;&lt;br /&gt;
            if (x &amp;lt;= array[mid])&lt;br /&gt;
                high = mid;&lt;br /&gt;
            else&lt;br /&gt;
                low = mid;&lt;br /&gt;
        }&lt;br /&gt;
        // now we have array[low] &amp;lt; x &amp;lt;= array[high]&lt;br /&gt;
        //             &amp;amp;&amp;amp; (low+1 == high || low == high)&lt;br /&gt;
        //  implies low+1 == high&lt;br /&gt;
        //debug.assertion (low+1 == high);&lt;br /&gt;
        return high;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Check whether the given Collection contains the given element instance. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
import java.util.Collection;&lt;br /&gt;
import java.util.Enumeration;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Properties;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2002-2007 the original author or authors.&lt;br /&gt;
 *&lt;br /&gt;
 * Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
 * you may not use this file except in compliance with the License.&lt;br /&gt;
 * You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Miscellaneous collection utility methods.&lt;br /&gt;
 * Mainly for internal use within the framework.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Juergen Hoeller&lt;br /&gt;
 * @author Rob Harrop&lt;br /&gt;
 * @since 1.1.3&lt;br /&gt;
 */&lt;br /&gt;
abstract class CollectionUtils {&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Check whether the given Collection contains the given element instance.&lt;br /&gt;
   * Enforces the given instance to be present, rather than returning&lt;br /&gt;
   * &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; for an equal element as well.&lt;br /&gt;
   * @param collection the Collection to check&lt;br /&gt;
   * @param element the element to look for&lt;br /&gt;
   * @return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if found, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; else&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean containsInstance(Collection collection, Object element) {&lt;br /&gt;
    if (collection != null) {&lt;br /&gt;
      for (Iterator it = collection.iterator(); it.hasNext();) {&lt;br /&gt;
        Object candidate = it.next();&lt;br /&gt;
        if (candidate == element) {&lt;br /&gt;
          return true;&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the supplied Collection is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * or empty. Otherwise, return &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param collection the Collection to check&lt;br /&gt;
   * @return whether the given Collection is empty&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isEmpty(Collection collection) {&lt;br /&gt;
    return (collection == null || collection.isEmpty());&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the supplied Map is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * or empty. Otherwise, return &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param map the Map to check&lt;br /&gt;
   * @return whether the given Map is empty&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isEmpty(Map map) {&lt;br /&gt;
    return (map == null || map.isEmpty());&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if any element in &amp;quot;&amp;lt;code&amp;gt;candidates&amp;lt;/code&amp;gt;&amp;quot; is&lt;br /&gt;
   * contained in &amp;quot;&amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt;&amp;quot;; otherwise returns &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param source the source Collection&lt;br /&gt;
   * @param candidates the candidates to search for&lt;br /&gt;
   * @return whether any of the candidates has been found&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean containsAny(Collection source, Collection candidates) {&lt;br /&gt;
    if (isEmpty(source) || isEmpty(candidates)) {&lt;br /&gt;
      return false;&lt;br /&gt;
    }&lt;br /&gt;
    for (Iterator it = candidates.iterator(); it.hasNext();) {&lt;br /&gt;
      if (source.contains(it.next())) {&lt;br /&gt;
        return true;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Return the first element in &amp;quot;&amp;lt;code&amp;gt;candidates&amp;lt;/code&amp;gt;&amp;quot; that is contained in&lt;br /&gt;
   * &amp;quot;&amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt;&amp;quot;. If no element in &amp;quot;&amp;lt;code&amp;gt;candidates&amp;lt;/code&amp;gt;&amp;quot; is present in&lt;br /&gt;
   * &amp;quot;&amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt;&amp;quot; returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;. Iteration order is&lt;br /&gt;
   * {@link Collection} implementation specific.&lt;br /&gt;
   * @param source the source Collection&lt;br /&gt;
   * @param candidates the candidates to search for&lt;br /&gt;
   * @return the first present object, or &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if not found&lt;br /&gt;
   */&lt;br /&gt;
  public static Object findFirstMatch(Collection source, Collection candidates) {&lt;br /&gt;
    if (isEmpty(source) || isEmpty(candidates)) {&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    for (Iterator it = candidates.iterator(); it.hasNext();) {&lt;br /&gt;
      Object candidate = it.next();&lt;br /&gt;
      if (source.contains(candidate)) {&lt;br /&gt;
        return candidate;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return null;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Find a value of the given type in the given Collection.&lt;br /&gt;
   * @param collection the Collection to search&lt;br /&gt;
   * @param type the type to look for&lt;br /&gt;
   * @return a value of the given type found, or &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if none&lt;br /&gt;
   * @throws IllegalArgumentException if more than one value of the given type found&lt;br /&gt;
   */&lt;br /&gt;
  public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException {&lt;br /&gt;
    if (isEmpty(collection)) {&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    Class typeToUse = (type != null ? type : Object.class);&lt;br /&gt;
    Object value = null;&lt;br /&gt;
    for (Iterator it = collection.iterator(); it.hasNext();) {&lt;br /&gt;
      Object obj = it.next();&lt;br /&gt;
      if (typeToUse.isInstance(obj)) {&lt;br /&gt;
        if (value != null) {&lt;br /&gt;
          throw new IllegalArgumentException(&amp;quot;More than one value of type [&amp;quot; + typeToUse.getName() + &amp;quot;] found&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        value = obj;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return value;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Determine whether the given Collection only contains a single unique object.&lt;br /&gt;
   * @param collection the Collection to check&lt;br /&gt;
   * @return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the collection contains a single reference or&lt;br /&gt;
   * multiple references to the same instance, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; else&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean hasUniqueObject(Collection collection) {&lt;br /&gt;
    if (isEmpty(collection)) {&lt;br /&gt;
      return false;&lt;br /&gt;
    }&lt;br /&gt;
    boolean hasCandidate = false;&lt;br /&gt;
    Object candidate = null;&lt;br /&gt;
    for (Iterator it = collection.iterator(); it.hasNext();) {&lt;br /&gt;
      Object elem = it.next();&lt;br /&gt;
      if (!hasCandidate) {&lt;br /&gt;
        hasCandidate = true;&lt;br /&gt;
        candidate = elem;&lt;br /&gt;
      }&lt;br /&gt;
      else if (candidate != elem) {&lt;br /&gt;
        return false;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return true;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Find a value of the given type in the given Collection ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
import java.util.Collection;&lt;br /&gt;
import java.util.Enumeration;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Properties;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2002-2007 the original author or authors.&lt;br /&gt;
 *&lt;br /&gt;
 * Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
 * you may not use this file except in compliance with the License.&lt;br /&gt;
 * You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Miscellaneous collection utility methods.&lt;br /&gt;
 * Mainly for internal use within the framework.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Juergen Hoeller&lt;br /&gt;
 * @author Rob Harrop&lt;br /&gt;
 * @since 1.1.3&lt;br /&gt;
 */&lt;br /&gt;
abstract class CollectionUtils {&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Find a value of the given type in the given Collection.&lt;br /&gt;
   * @param collection the Collection to search&lt;br /&gt;
   * @param type the type to look for&lt;br /&gt;
   * @return a value of the given type found, or &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if none&lt;br /&gt;
   * @throws IllegalArgumentException if more than one value of the given type found&lt;br /&gt;
   */&lt;br /&gt;
  public static Object findValueOfType(Collection collection, Class type) throws IllegalArgumentException {&lt;br /&gt;
    if (isEmpty(collection)) {&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    Class typeToUse = (type != null ? type : Object.class);&lt;br /&gt;
    Object value = null;&lt;br /&gt;
    for (Iterator it = collection.iterator(); it.hasNext();) {&lt;br /&gt;
      Object obj = it.next();&lt;br /&gt;
      if (typeToUse.isInstance(obj)) {&lt;br /&gt;
        if (value != null) {&lt;br /&gt;
          throw new IllegalArgumentException(&amp;quot;More than one value of type [&amp;quot; + typeToUse.getName() + &amp;quot;] found&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        value = obj;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return value;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the supplied Collection is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * or empty. Otherwise, return &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param collection the Collection to check&lt;br /&gt;
   * @return whether the given Collection is empty&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isEmpty(Collection collection) {&lt;br /&gt;
    return (collection == null || collection.isEmpty());&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the supplied Map is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * or empty. Otherwise, return &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param map the Map to check&lt;br /&gt;
   * @return whether the given Map is empty&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isEmpty(Map map) {&lt;br /&gt;
    return (map == null || map.isEmpty());&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Return the first element in &amp;quot;&amp;lt;code&amp;gt;candidates&amp;lt;/code&amp;gt;&amp;quot; that is contained in&lt;br /&gt;
   * &amp;quot;&amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt;&amp;quot;. If no element in &amp;quot;&amp;lt;code&amp;gt;candidates&amp;lt;/code&amp;gt;&amp;quot; is present in&lt;br /&gt;
   * &amp;quot;&amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt;&amp;quot; returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;. Iteration order is&lt;br /&gt;
   * {@link Collection} implementation specific.&lt;br /&gt;
   * @param source the source Collection&lt;br /&gt;
   * @param candidates the candidates to search for&lt;br /&gt;
   * @return the first present object, or &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if not found&lt;br /&gt;
   */&lt;br /&gt;
  public static Object findFirstMatch(Collection source, Collection candidates) {&lt;br /&gt;
    if (isEmpty(source) || isEmpty(candidates)) {&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    for (Iterator it = candidates.iterator(); it.hasNext();) {&lt;br /&gt;
      Object candidate = it.next();&lt;br /&gt;
      if (source.contains(candidate)) {&lt;br /&gt;
        return candidate;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return null;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Determine whether the given Collection only contains a single unique object.&lt;br /&gt;
   * @param collection the Collection to check&lt;br /&gt;
   * @return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the collection contains a single reference or&lt;br /&gt;
   * multiple references to the same instance, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; else&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean hasUniqueObject(Collection collection) {&lt;br /&gt;
    if (isEmpty(collection)) {&lt;br /&gt;
      return false;&lt;br /&gt;
    }&lt;br /&gt;
    boolean hasCandidate = false;&lt;br /&gt;
    Object candidate = null;&lt;br /&gt;
    for (Iterator it = collection.iterator(); it.hasNext();) {&lt;br /&gt;
      Object elem = it.next();&lt;br /&gt;
      if (!hasCandidate) {&lt;br /&gt;
        hasCandidate = true;&lt;br /&gt;
        candidate = elem;&lt;br /&gt;
      }&lt;br /&gt;
      else if (candidate != elem) {&lt;br /&gt;
        return false;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return true;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get the difference of two collections ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collection;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Utils {&lt;br /&gt;
  public static &amp;lt;T&amp;gt; Collection&amp;lt;T&amp;gt; diff(Collection&amp;lt;T&amp;gt; c1, Collection&amp;lt;T&amp;gt; c2) {&lt;br /&gt;
    if (c1 == null || c1.size() == 0 || c2 == null || c2.size() == 0) {&lt;br /&gt;
        return c1;&lt;br /&gt;
    }&lt;br /&gt;
    Collection&amp;lt;T&amp;gt; difference = new ArrayList&amp;lt;T&amp;gt;();&lt;br /&gt;
    for (T item : c1) {&lt;br /&gt;
        if (!c2.contains(item)) {&lt;br /&gt;
            difference.add(item);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    return difference;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Return the first element in &amp;quot;candidates&amp;quot; that is contained in source ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
import java.util.Collection;&lt;br /&gt;
import java.util.Enumeration;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Properties;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2002-2007 the original author or authors.&lt;br /&gt;
 *&lt;br /&gt;
 * Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
 * you may not use this file except in compliance with the License.&lt;br /&gt;
 * You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Miscellaneous collection utility methods.&lt;br /&gt;
 * Mainly for internal use within the framework.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Juergen Hoeller&lt;br /&gt;
 * @author Rob Harrop&lt;br /&gt;
 * @since 1.1.3&lt;br /&gt;
 */&lt;br /&gt;
abstract class CollectionUtils {&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Return the first element in &amp;quot;&amp;lt;code&amp;gt;candidates&amp;lt;/code&amp;gt;&amp;quot; that is contained in&lt;br /&gt;
   * &amp;quot;&amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt;&amp;quot;. If no element in &amp;quot;&amp;lt;code&amp;gt;candidates&amp;lt;/code&amp;gt;&amp;quot; is present in&lt;br /&gt;
   * &amp;quot;&amp;lt;code&amp;gt;source&amp;lt;/code&amp;gt;&amp;quot; returns &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;. Iteration order is&lt;br /&gt;
   * {@link Collection} implementation specific.&lt;br /&gt;
   * @param source the source Collection&lt;br /&gt;
   * @param candidates the candidates to search for&lt;br /&gt;
   * @return the first present object, or &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if not found&lt;br /&gt;
   */&lt;br /&gt;
  public static Object findFirstMatch(Collection source, Collection candidates) {&lt;br /&gt;
    if (isEmpty(source) || isEmpty(candidates)) {&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    for (Iterator it = candidates.iterator(); it.hasNext();) {&lt;br /&gt;
      Object candidate = it.next();&lt;br /&gt;
      if (source.contains(candidate)) {&lt;br /&gt;
        return candidate;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return null;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the supplied Collection is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * or empty. Otherwise, return &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param collection the Collection to check&lt;br /&gt;
   * @return whether the given Collection is empty&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isEmpty(Collection collection) {&lt;br /&gt;
    return (collection == null || collection.isEmpty());&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if the supplied Map is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * or empty. Otherwise, return &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param map the Map to check&lt;br /&gt;
   * @return whether the given Map is empty&lt;br /&gt;
   */&lt;br /&gt;
  public static boolean isEmpty(Map map) {&lt;br /&gt;
    return (map == null || map.isEmpty());&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>