文字列など、いくつかのアイテムのリストのリストを提供しているとしましょう。
list 1: "a", "b", "c"
list 2: "d", "e", "f"
list 3: "1", "2", "3"
results: (a, d, 1), (a, d, 2), ... (c, f, 3)
(実際のユースケースは文字列などとは関係ありません。これは単なるモックアップです)
私はそれを行うための再帰的なメソッドを作成しましたが、それが投げられる一時的なセットをたくさん作成するので、私はそれに満足していません(ええ、オブジェクトの作成はJavaで安価であり、通常はCのmallocよりもCPU命令が少ないことを知っています(ソース:Java Concurrency in Action、p241)、eden GCは安い、何とか何とか何とか。ユーモアを交えて:)。
void combine(List<List<String>> itemLists, List<Set<String>> combinations, Set<String> partial) {
if (itemLists == null || itemLists.isEmpty()) return;
List<String> items = itemLists.get(0);
for (String s : items) {
Set<String> tmpSet = new HashSet<>(partial);
tmpSet.add(s);
if (itemLists.size() == 0) //termination test
combinations.add(tmpSet);
else
combine(itemLists.subList(1, itemLists.size()), combinations, tmpSet);
}
}
それで、あなたはこれについてどうしますか?
編集:明確にするために、私は順列を作成したくありません。sizeof(リストのリスト)が大きいセットを作成したい。