Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static <T extends Comparable<T>> List<List<T>> subsets(List<T> list)
- {
- List<List<T>> returnValue;
- if ( list.isEmpty() )
- {
- List<List<T>> lists = new ArrayList<List<T>>();
- lists.add(list);
- returnValue = lists;
- } else if ( list.size() == 1 )
- {
- List<List<T>> lists = new ArrayList<List<T>>();
- lists.add(list);
- lists.add(new ArrayList<T>());
- returnValue = lists;
- }
- else
- {
- List<T> firstElement = list.subList(0,1);
- List<T> rest = list.subList( 1, list.size() );
- returnValue = cartesian(subsets(firstElement), subsets(rest));
- }
- return returnValue;
- }
- public static <T extends Comparable<T>> List<List<T>> cartesian(List<List<T>> left, List<List<T>> right)
- {
- List<List<T>> returnValue = new ArrayList<>();
- if (left != null && right != null)
- {
- for ( List<T> leftElement : left )
- {
- for ( List<T> rightElement : right )
- {
- returnValue.add(Stream.concat( leftElement.stream(), rightElement.stream() ).collect(Collectors.toList()));
- }
- }
- }
- return returnValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement