프로그램/java

String 이 List안에 있는지 확인하는 Utility

mulderu 2014. 4. 22. 12:15

String 이 List안에 있는지 확인하는 Utility

apache common collections 의 CollectionUtils  이용하는 방법을 소개 합니다.

3가지 많이쓰는 매칭 방법을 이용하도록 하였습니다. :)



import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

/**
     * List안에 checkString  이 있는지 확인,
     * 
     * @param list
     * @param checkString
     * @param checkMethod -1: startsWidth, 0: equals , 1: endsWith
     * @return
     */
    public static boolean existsInStringList (List list, final String checkString, final int checkMethod)
    {
        if (CollectionUtils.exists(list, new Predicate() {
            
            public boolean evaluate(Object arg0) {
                String element = (String) arg0;
                
                if (checkMethod < 0 && checkString.startsWith(element)) 
                {
                    return true;
                }
                else if (checkMethod == 0 && checkString.equals(element)) 
                {
                    return true;
                }
                else if (checkMethod > 0 && checkString.endsWith(element)) 
                {
                    return true;
                }
                else return false;
            }
        })) 
        {
            return true;    
        }
        else
        {
            return false;
        }
    }