import java.util.stream.*; class KV { String ch; int id; } public class Test { private static void print(String text, int offset) { IntStream.range(0, text.length()) .mapToObj(i -> new KV(){{ch = text.substring(i, i + 1); id = i;}}) .collect(Collectors.groupingBy(kv -> kv.id % offset)) .forEach((k, kvs) -> System.out.println( kvs.stream().map(kv -> kv.ch) .reduce((acc, s) -> s + "|" + acc).get())); } public static void main(String[] args) { print("床前明月光疑是地上霜举头望明月低头思故乡", 5); } } //低|举|疑|床 //头|头|是|前 //思|望|地|明 //故|明|上|月 //乡|月|霜|光
趣味编程:静夜思(Java8版),布布扣,bubuko.com
原文地址:http://blog.csdn.net/zwvista/article/details/26615949