For example we have this kind of list ("A", "B", "C", "D"). We need to compare each one with another, not duplicate comparisons for example output should be like that.
- "A" with "B", "C" with "D"
- "A" with "C", "B" with "D"
"A" with "D", "B" with "C"
List<String> list = Arrays.asList("A", "B", "C", "D"); for (int i = 0; i < list.size(); i++) { for (int j = i + 1; j < list.size(); j++) { System.out.print(" Compare " + list.get(i) + " " + list.get(j)); } System.out.println(); }
This code return all combination, but not grouped.