Here you can find the source of stringCrossJoin(List
private static List<String> stringCrossJoin(List<Set<String>> candidates)
//package com.java2s; /**/*from w ww. ja va2 s . co m*/ * Copyright 2011-2017 Asakusa Framework Team. * * 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. */ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Set; public class Main { private static List<String> stringCrossJoin(List<Set<String>> candidates) { assert candidates != null; assert candidates.isEmpty() == false; List<String> results = new ArrayList<>(); Iterator<Set<String>> iter = candidates.iterator(); assert iter.hasNext(); results.addAll(iter.next()); while (iter.hasNext()) { Set<String> next = iter.next(); if (next.size() == 1) { String suffix = next.iterator().next(); for (ListIterator<String> i = results.listIterator(); i.hasNext();) { String vaule = i.next(); i.set(vaule + suffix); } } else { List<String> nextResults = new ArrayList<>(); for (String value : results) { for (String suffix : next) { nextResults.add(value + suffix); } } results = nextResults; } } return results; } }