Skip to content

Instantly share code, notes, and snippets.

@skht777
Last active December 30, 2016 02:46
Show Gist options
  • Select an option

  • Save skht777/53b9725905f5dbf796e131e957119406 to your computer and use it in GitHub Desktop.

Select an option

Save skht777/53b9725905f5dbf796e131e957119406 to your computer and use it in GitHub Desktop.
eclipse-collectionsを用いたzipWith
import org.eclipse.collections.impl.factory.Lists;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
/**
* @author skht777
*/
public class ECZipWith {
public static <T, S, U> ImmutableList<U> zipWith(BiFunction<T, S, U> f, List<T> l1, List<S> l2) {
return Lists.immutable.ofAll(l1).zip(l2).collect(p -> f.apply(p.getOne(), p.getTwo()));
}
public static String StrZipWith(BiFunction<String, String, String> f, String s1, String s2) {
return Lists.immutable.ofAll(zipWith(f, strToList(s1), strToList(s2))).makeString("");
}
private static List<String> strToList(String s) {
return s.chars().mapToObj(c -> (char) c).map(String::valueOf).collect(Collectors.toList());
}
public static void main(String[] args) {
final String pt = "パトカー";
final String tx = "タクシー";
System.out.println(StrZipWith(((p, t) -> p + t), pt, tx));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment