如何实现通过一个集合来生成有序项集的所有组合

import java.util.*;

public class Combination {
    public static > List>
    findSortedCombinations(Collection elements, int n){
        List> result = new ArrayList>();
        if(n == 0){
            result.add(new ArrayList());
            return result;
        }

        List> combinations = findSortedCombinations(elements, n - 1);
        System.out.println("回的次数");
        for(List combination : combinations){
            System.out.println("combination\t"+combination);
            for(T element : elements){
                System.out.println(element);
                if(combination.contains(element)){
                    System.out.println("combination包含了"+element+"\t"+combination);
                    continue;
                }

                List list = new ArrayList();
                list.addAll(combination);
                if(list.contains(element)){
                    System.out.println("list包含了"+element);
                    continue;
                }

                list.add(element);
                Collections.sort(list);
                if(result.contains(list)){
                    System.out.println("result包含了"+list+"\t"+result);
                    continue;
                }
                result.add(list);
                System.out.println(list+"\t\t"+result);

            }

        }

        return result;
    }

    public static > List>
    findSortedCombinations(Collection elements){
        List> result = new ArrayList>();
        for(int i = 0; i <= elements.size(); i++){
            result.addAll(findSortedCombinations(elements,i));
        }
        return result;
    }

    public static void main(String[] args) {
        List list = Arrays.asList("a", "b", "c", "d");
        System.out.println("list="+ list);
        List> comb = findSortedCombinations(list, 2);
        System.out.println(comb.size());
        System.out.println(comb);

    }


}

 

你可能感兴趣的:(大数据,java)