This method checks if the list contains the given string (case sensitive). - Java java.util

Java examples for java.util:List Contain

Description

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

Demo Code


//package com.book2s;

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

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "book2s.com");
        String string = "book2s.com";
        System.out.println(contains(list, string));
    }/*w w  w . ja  va  2s . c  o m*/

    /**
     * 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 Tutorials