Skip to content

Instantly share code, notes, and snippets.

@rac021
Created July 25, 2017 13:08
Show Gist options
  • Save rac021/3ab8b37e6df7a8cd057cf41f71d21974 to your computer and use it in GitHub Desktop.
Save rac021/3ab8b37e6df7a8cd057cf41f71d21974 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
cartesianProduct ( Arrays.asList (
Arrays.asList( 1900, 1901, 1902 ) ,
Arrays.asList( Arrays.asList("R","Y") , Arrays.asList("RR","YY") ) ,
Arrays.asList(Arrays.asList("10") )
)
) ;
} ;
public static <T> void cartesianProduct ( List<List<T>> lists ) {
int solutions = 1 ;
for(int i = 0; i < lists.size(); i++ ) solutions *= lists.get(i).size() ;
System.out.println(" Total combination : " + solutions + "\n " ) ;
for(int i = 0; i < solutions; i++) {
int j = 1 ;
List<T> combined = new ArrayList<>();
for( List<T> list : lists) {
if( list.get((i/j) % list.size() ) instanceof List) {
combined.addAll( (List) list.get( (i/j) % list.size() ) ) ;
}
else {
combined.add( list.get( (i/j) % list.size() ) ) ;
}
j *= list.size() ;
}
System.out.println(combined) ;
System.out.println() ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment