Here you can find the source of recursiveCombine(List>> toCombinate, int index)
private static <T> List<List<T>> recursiveCombine(List<List<List<T>>> toCombinate, int index)
//package com.java2s; /* Copyright (C) 2011 SpringSource * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License./* ww w . j ava 2 s . c om*/ */ import java.util.ArrayList; import java.util.List; public class Main { private static <T> List<List<T>> recursiveCombine(List<List<List<T>>> toCombinate, int index) { if (index == toCombinate.size() - 1) { //this is leaf, just return what we got at this position return new ArrayList<List<T>>(toCombinate.get(index)); } else { List<List<T>> next = recursiveCombine(toCombinate, index + 1); //now combinate with each element in my list List<List<T>> result = new ArrayList<List<T>>(); List<List<T>> currentContainer = toCombinate.get(index); //[ [a],[b] ] for (List<T> current : currentContainer) { for (List<T> n : next) { List<T> temp = new ArrayList<T>(n); temp.addAll(current); result.add(temp); } } return result; } } }