import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
class Splitting {
public static void main
(String[] args
) { Map
<String, Integer
> map
= new HashMap
<>(); map.put("a", 1);
map.put("b", 2);
for(int depth
: new int[] { 1,
2,
Integer.
MAX_VALUE }) { System.
out.
println("With max depth: "+depth
); Tree
<Spliterator
<Map.
Entry<String, Integer
>>> spTree
= split(map.entrySet().spliterator(), depth);
Tree<String> valueTree = spTree.map(sp -> "estimated: "+sp.estimateSize()+" "
+StreamSupport.stream(sp, false).collect(Collectors.toList()));
System.
out.
println(valueTree
); }
}
private static <T> Tree<Spliterator<T>> split(Spliterator<T> sp, int depth) {
Spliterator<T> prefix = depth-- > 0? sp.trySplit(): null;
return prefix == null?
new Tree<>(sp): new Tree<>(null, split(prefix, depth), split(sp, depth));
}
public static class Tree<T> {
final T value;
List<Tree<T>> children;
public Tree(T value) {
this.value = value;
}
public Tree(T value, Tree<T>... ch) {
this.value = value;
}
public <U> Tree<U> map(Function<? super T, ? extends U> f) {
Tree<U> t = new Tree<>(value == null? null: f.apply(value));
if(!children.isEmpty()) {
t.children = new ArrayList<>(children.size());
for(Tree<T> ch: children) t.children.add(ch.map(f));
}
return t;
}
public @Override
String toString
() { if(children.isEmpty()) return value == null? "": value.toString();
final StringBuilder sb = new StringBuilder(100);
toString(sb, 0, 0);
return sb.toString();
}
public void toString(StringBuilder sb, int preS, int preEnd) {
final int myHandle = sb.length() - 2;
sb.append(value == null? "": value).append('\n');
final int num = children.size() - 1;
if (num >= 0) {
if (num != 0) {
for (int ix = 0; ix < num; ix++) {
int nPreS = sb.length();
sb.append(sb, preS, preEnd);
sb.append("\u2502 ");
int nPreE = sb.length();
children.get(ix).toString(sb, nPreS, nPreE);
}
}
int nPreS = sb.length();
sb.append(sb, preS, preEnd);
final int lastItemHandle = sb.length();
sb.append(" ");
int nPreE = sb.length();
children.get(num).toString(sb, nPreS, nPreE);
sb.setCharAt(lastItemHandle, '\u2514');
}
if (myHandle > 0)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oaWxkcmVuLnNpemUoKSk7CiAgICAgICAgICAgICAgICBmb3IoVHJlZTxUPiBjaDogY2hpbGRyZW4pIHQuY2hpbGRyZW4uYWRkKGNoLm1hcChmKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIHQ7CiAgICAgICAgfQogICAgICAgIHB1YmxpYyBAT3ZlcnJpZGUgU3RyaW5nIHRvU3RyaW5nKCkgewogICAgICAgICAgICBpZihjaGlsZHJlbi5pc0VtcHR5KCkpIHJldHVybiB2YWx1ZSA9PSBudWxsPyAiIjogdmFsdWUudG9TdHJpbmcoKTsKICAgICAgICAgICAgZmluYWwgU3RyaW5nQnVpbGRlciBzYiA9IG5ldyBTdHJpbmdCdWlsZGVyKDEwMCk7CiAgICAgICAgICAgIHRvU3RyaW5nKHNiLCAwLCAwKTsKICAgICAgICAgICAgcmV0dXJuIHNiLnRvU3RyaW5nKCk7CiAgICAgICAgfQogICAgICAgIHB1YmxpYyB2b2lkIHRvU3RyaW5nKFN0cmluZ0J1aWxkZXIgc2IsIGludCBwcmVTLCBpbnQgcHJlRW5kKSB7CiAgICAgICAgICAgIGZpbmFsIGludCBteUhhbmRsZSA9IHNiLmxlbmd0aCgpIC0gMjsKICAgICAgICAgICAgc2IuYXBwZW5kKHZhbHVlID09IG51bGw/ICIiOiB2YWx1ZSkuYXBwZW5kKCdcbicpOwogICAgICAgICAgICBmaW5hbCBpbnQgbnVtID0gY2hpbGRyZW4uc2l6ZSgpIC0gMTsKICAgICAgICAgICAgaWYgKG51bSA+PSAwKSB7CiAgICAgICAgICAgICAgICBpZiAobnVtICE9IDApIHsKICAgICAgICAgICAgICAgICAgICBmb3IgKGludCBpeCA9IDA7IGl4IDwgbnVtOyBpeCsrKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGludCBuUHJlUyA9IHNiLmxlbmd0aCgpOwogICAgICAgICAgICAgICAgICAgICAgICBzYi5hcHBlbmQoc2IsIHByZVMsIHByZUVuZCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHNiLmFwcGVuZCgiXHUyNTAyICIpOwogICAgICAgICAgICAgICAgICAgICAgICBpbnQgblByZUUgPSBzYi5sZW5ndGgoKTsKICAgICAgICAgICAgICAgICAgICAgICAgY2hpbGRyZW4uZ2V0KGl4KS50b1N0cmluZyhzYiwgblByZVMsIG5QcmVFKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpbnQgblByZVMgPSBzYi5sZW5ndGgoKTsKICAgICAgICAgICAgICAgIHNiLmFwcGVuZChzYiwgcHJlUywgcHJlRW5kKTsKICAgICAgICAgICAgICAgIGZpbmFsIGludCBsYXN0SXRlbUhhbmRsZSA9IHNiLmxlbmd0aCgpOwogICAgICAgICAgICAgICAgc2IuYXBwZW5kKCIgICIpOwogICAgICAgICAgICAgICAgaW50IG5QcmVFID0gc2IubGVuZ3RoKCk7CiAgICAgICAgICAgICAgICBjaGlsZHJlbi5nZXQobnVtKS50b1N0cmluZyhzYiwgblByZVMsIG5QcmVFKTsKICAgICAgICAgICAgICAgIHNiLnNldENoYXJBdChsYXN0SXRlbUhhbmRsZSwgJ1x1MjUxNCcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChteUhhbmRsZSA+IDApIHsKICAgICAgICAgICAgICAgIHNiLnNldENoYXJBdChteUhhbmRsZSwgJ1x1MjUxYycpOwogICAgICAgICAgICAgICAgc2Iuc2V0Q2hhckF0KG15SGFuZGxlICsgMSwgJ1x1MjUwMCcpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9Cg==