Click here to Skip to main content
14,271,317 members
Rate this:
Please Sign up or sign in to vote.
See more:
import java.util.*;

public class lISTSS {

public static <t> void getCombination(List<t>... lists) {
if (lists == null) return;
getCombinations(new ArrayList<t>(), 0, lists);

private static <t> void getCombinations(List<t> soFar, int i, List<t>... lists) {

if (i == lists.length) { //no more lists left:


} else {

for (T t : lists[i]) {

soFar.add(t); //"guess" item
getCombinations(soFar, i + 1, lists); //recurse on rest of lists
soFar.remove(soFar.size() - 1); //cleanup

public static void main(String args[]) {

List<string> A = Arrays.asList("one","two","three");
List<string> B = Arrays.asList("four","five");


What I have tried:

This is the current output:

[one, four]
[one, five]
[two, four]
[two, five]
[three, four]
[three, five]
I have the above code that gets all combinations of two lists, and I got the output, but I want to remove the combinations [one, five] and [two, four]. I've tried the following without avail.

if(soFar.get(i).equals("one, five")) {
soFar.remove("one, five");
Updated 4-Apr-16 19:55pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to use Iterator and call remove() on iterator instead of using for loop.

Iterator (Java Platform SE 7 )[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100