简体   繁体   中英

how to compare all possible combination in arraylist java

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.

  1. "A" with "B", "C" with "D"
  2. "A" with "C", "B" with "D"
  3. "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.

To achieve your "grouping" you can compare A and B then remove them from the list, and compare the remaining which will be C and D. Then compare A and C, remove them and the compare the remaining which will be B and D. and etc.

You can do this recursively for any list only if the number of elements is even, because after removing 2 elements from the list, you want the remaining number of elements to be a multiple of 2, or you will be left with 1 element at the end

Here is something you can do, compare each element of the list with all the elements, and add the two to a list of String , to prevent duplications ( it will see if these 2 exist already before comparing them ):

List<String> list = Arrays.asList("A", "B", "C", "D");
List<String> existing = new ArrayList<String>();

for ( String element : list ) {
    for ( String element2 : list ) {
        if (!existing.contains(element+element2)) {
             System.out.println(" Compare "+element+" with "+element2);
             existing.add(element+element2);
           }
    }
}

Effectively, you are looking into the tournament scheduling problem. Comparison is a match, and grouping forms a round. There are many ways to do so, and you may start here .

You can do solve it as follows:

  1. Create a new List eg result as shown in the example below.
  2. Using your nested loops, create a combination of list.get(i) and list.get(j) . Then, check if this combination of its reverse is already there in result ; if not, add this combination to result .

     import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C", "D"); List<String> result = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { for (int j = i + 1; j < list.size(); j++) { String combination = list.get(i) + " " + list.get(j); if (.result.contains(combination) &&.result.contains(new StringBuilder(combination).reverse();toString())) { result:add(combination). } } } // Display the result for (String combination. result) { System;out.println("Compare " + combination); } } }

    Output:

     Compare AB Compare A C Compare AD Compare B C Compare BD Compare C D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM