-1

Please Suggest How to check Strings equality which are in Uppercase and Lower Case using List Contains Method

example:-

    List ll = new  ArrayList();
    ll.add("WWW");
    ll.add("XXX");
    ll.add("YYY");
    ll.add("ZZZ");


    System.out.println(ll.contains("xxx")); // Which returns the false I wanna return true Here

Here My List Size is Much Bigger Means around 1000000 , So I cannot use the For Loop to check String using equalsIgnoreCase, Also I doesnt Wants to Write My custom List.

Omkar More
  • 3
  • 1
  • 4

1 Answers1

-1

You can't avoid for loop. Even "basic" contains method uses for loop to compare each element of a list.

Here's contains method code of ArrayList class :

    /**
     * Returns <tt>true</tt> if this list contains the specified element.
     * More formally, returns <tt>true</tt> if and only if this list contains
     * at least one element <tt>e</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
     *
     * @param o element whose presence in this list is to be tested
     * @return <tt>true</tt> if this list contains the specified element
     */
    public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }

    /**
     * Returns the index of the first occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the lowest index <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     */
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
Mickael
  • 4,458
  • 2
  • 28
  • 40
  • Thnxxx So i have to write Custom List And use Equal IgnoreCase for String Equality. – Omkar More Dec 13 '16 at 11:16
  • Yes. I think you actually have no choice. – Mickael Dec 13 '16 at 11:19
  • Not completely right I afraid: the first premise of this ticket is that list contains such an amount of elements that looping through it and to use the equalsIgnoreCase is not a solution. Well, I do not share this opinion but if we have to accept this starting point, your solution would only avoid the loop overriding the method to add a comparison with null and equals.Case differences would not be considered so it would not work fine. In addition to this this option means to create a custom List from AbstractList which all advantages and disadvantages related to in design and development – Facepalmed Dec 13 '16 at 11:23
  • If I understand your remark correctly, I agree on the fact that you should avoid the situation where you will have to compare a list with that many elements. This problem is most likely the consequence of a conception issue. Regarding the strings comparison, there're good api about this like StringUtils (https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#compareIgnoreCase-java.lang.String-java.lang.String-) – Mickael Dec 13 '16 at 11:31