単純にリストの項目を要素2個のペアで、全組み合わせの処理をするのに、数式 nCr に相当する処理を書いていたので
沢山の処理になってしまう。
要素2個のペアの処理を全組み合わせを単純に行いたい。
サンプル
List<String> list = Arrays.asList( "a", "b", "c", "d", "e"); AtomicInteger i = new AtomicInteger(0); list.stream().forEach(e->{ list.subList(i.incrementAndGet(), list.size()).stream().forEach(s->{ // ペア e と s の処理を行う。 System.out.println( e + " - " + s ); }); });
結果
a - b a - c a - d a - e b - c b - d b - e c - d c - e d - e
単に、subList を stream の処理で流してるだけだが。