Java List Contain contains(List list, String string)

Here you can find the source of contains(List list, String string)

Description

This method checks if the list contains the given string (case sensitive).

License

Open Source License

Parameter

Parameter Description
list the list to check
string the string

Return

true/false

Declaration

public static boolean contains(List<String> list, String string) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;
import java.util.Set;

public class Main {
    /**/* w w  w  . j  ava  2  s.  com*/
     * This method checks if the list contains the given string (case sensitive).
     * 
     * @param list
     *          the list to check
     * @param string
     *          the string
     * @return true/false
     */
    public static boolean contains(List<String> list, String string) {
        if (isNotEmpty(list)) {
            for (String item : list) {
                if ((item != null && string != null && item.compareTo(string) == 0)
                        || (item == null && string == null)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Check if set isn't empty.
     * 
     * @param set
     *          The set to be tested.
     * @return <code>true</code> is not empty, <code>false</code> otherwise.
     */
    public static <E> boolean isNotEmpty(Set<E> set) {
        return set != null && !set.isEmpty();
    }

    /**
     * This method checks if the list is not null and have elements.
     * 
     * @param <E>
     * 
     * @param list
     *          the list to check
     * @return true/false
     */
    public static <E> boolean isNotEmpty(List<E> list) {
        return list != null && !list.isEmpty();
    }

    /**
     * This method checks if the list is not null and have elements.
     * 
     * @param <E>
     * 
     * @param list
     *          the list to check
     * @return true/false
     */
    public static <E> boolean isNotEmpty(E[] list) {
        return list != null && list.length > 0;
    }

    /**
     * Check if set is empty.
     * 
     * @param set
     * @return
     */
    public static <E> boolean isEmpty(Set<E> set) {
        return !isNotEmpty(set);
    }

    public static <E> boolean isEmpty(List<E> list) {
        return !isNotEmpty(list);
    }

    public static <E> boolean isEmpty(E[] list) {
        return !isNotEmpty(list);
    }
}

Related

  1. contains(List objects, Object object)
  2. contains(List listOfArrays, A[] array)
  3. contains(List list, int[] arr)
  4. contains(List list, List> listOfLists)
  5. contains(List list, String str, boolean bcaseSensitive)
  6. contains(List list1, List list2)
  7. contains(List container, List list, Comparator comparator)
  8. contains(String str, List list)
  9. contains1(final String id, final List idList)