Elegant solution to find a pair of Strings in a List I'm new to Java8 and I must find an elegant solution to a problem. The problem goes I have a List of letters (of any size) that could contain any letter such as "A", "B", "C", "D", etc. I must check if I have an pair of "A" and "B". So, if I have something like ["A", "A", "C", "Z"], since I don't have both "A" and "B" the code should throw an error. I need to refactor something like this in java8...

import java.util.Arrays;
import java.util.List;

public class atest {

    public static void checkForABPair(List&lt;String&gt; letters) {
        int iA = 0;
        int iB = 0;
        for (String letter : letters) {
            if (letter.equals("A")) {
                iA = 1;
            }
            if (letter.equals("B")) {
                iB = 1;
            }
        }
        if ((1 == iA || 1 == iB) &amp;&amp; (1 != iA || 1 != iB)) {
            System.out.println(letters + " - ERROR! The pair of letters [A - B] must be present at least once, or not at all!");
        } else {
            System.out.println(letters + " - Everything fine!");
        }
    }

    public static void main(String[] args) {

        List&lt;String&gt; letters = Arrays.asList("B", "A", "C");
        checkForABPair(letters);

        letters = Arrays.asList("A", "A", "C");
        checkForABPair(letters);

        letters = Arrays.asList("M", "S", "C");
        checkForABPair(letters);
    }
}


This must return the following:

[B, A, C] - Everything fine!
[A, A, C] - ERROR! The pair of letters [A - B] must be present at least once, or not at all!
[M, S, C] - Everything fine!


I need it done in a more elegant way. Anyone have a more elegant solution? Thanks!
iA and iB can only take the values 0 and 1, so that

if ((1 == iA || 1 == iB) &amp;&amp; (1 != iA || 1 != iB)) { ... }


can be simplified to

if (iA != iB) { ... }


Boolean variables would be more appropriate for this purpose, and the variable names can be improved, e.g.

boolean containsA = false;
boolean containsB = false;