fork download
  1.  
  2. import java.util.*;
  3. import java.util.function.*;
  4. import java.util.stream.Collectors;
  5. import java.util.stream.StreamSupport;
  6.  
  7. class Splitting {
  8. public static void main(String[] args) {
  9. Map<String, Integer> map = new HashMap<>();
  10. map.put("a", 1);
  11. map.put("b", 2);
  12.  
  13. for(int depth: new int[] { 1, 2, Integer.MAX_VALUE }) {
  14. System.out.println("With max depth: "+depth);
  15. Tree<Spliterator<Map.Entry<String, Integer>>> spTree
  16. = split(map.entrySet().spliterator(), depth);
  17. Tree<String> valueTree = spTree.map(sp -> "estimated: "+sp.estimateSize()+" "
  18. +StreamSupport.stream(sp, false).collect(Collectors.toList()));
  19. System.out.println(valueTree);
  20. }
  21. }
  22.  
  23. private static <T> Tree<Spliterator<T>> split(Spliterator<T> sp, int depth) {
  24. Spliterator<T> prefix = depth-- > 0? sp.trySplit(): null;
  25. return prefix == null?
  26. new Tree<>(sp): new Tree<>(null, split(prefix, depth), split(sp, depth));
  27. }
  28.  
  29. public static class Tree<T> {
  30. final T value;
  31. List<Tree<T>> children;
  32.  
  33. public Tree(T value) {
  34. this.value = value;
  35. children = Collections.emptyList();
  36. }
  37. public Tree(T value, Tree<T>... ch) {
  38. this.value = value;
  39. children = Arrays.asList(ch);
  40. }
  41. public <U> Tree<U> map(Function<? super T, ? extends U> f) {
  42. Tree<U> t = new Tree<>(value == null? null: f.apply(value));
  43. if(!children.isEmpty()) {
  44. t.children = new ArrayList<>(children.size());
  45. for(Tree<T> ch: children) t.children.add(ch.map(f));
  46. }
  47. return t;
  48. }
  49. public @Override String toString() {
  50. if(children.isEmpty()) return value == null? "": value.toString();
  51. final StringBuilder sb = new StringBuilder(100);
  52. toString(sb, 0, 0);
  53. return sb.toString();
  54. }
  55. public void toString(StringBuilder sb, int preS, int preEnd) {
  56. final int myHandle = sb.length() - 2;
  57. sb.append(value == null? "": value).append('\n');
  58. final int num = children.size() - 1;
  59. if (num >= 0) {
  60. if (num != 0) {
  61. for (int ix = 0; ix < num; ix++) {
  62. int nPreS = sb.length();
  63. sb.append(sb, preS, preEnd);
  64. sb.append("\u2502 ");
  65. int nPreE = sb.length();
  66. children.get(ix).toString(sb, nPreS, nPreE);
  67. }
  68. }
  69. int nPreS = sb.length();
  70. sb.append(sb, preS, preEnd);
  71. final int lastItemHandle = sb.length();
  72. sb.append(" ");
  73. int nPreE = sb.length();
  74. children.get(num).toString(sb, nPreS, nPreE);
  75. sb.setCharAt(lastItemHandle, '\u2514');
  76. }
  77. if (myHandle > 0) {
  78. sb.setCharAt(myHandle, '\u251c');
  79. sb.setCharAt(myHandle + 1, '\u2500');
  80. }
  81. }
  82. }
  83. }
  84.  
Success #stdin #stdout 0.13s 36792KB
stdin
Standard input is empty
stdout
With max depth: 1

├─estimated: 1 [a=1, b=2]
└─estimated: 1 []

With max depth: 2

├─
│ ├─estimated: 0 [a=1, b=2]
│ └─estimated: 0 []
└─
  ├─estimated: 0 []
  └─estimated: 0 []

With max depth: 2147483647

├─
│ ├─
│ │ ├─
│ │ │ ├─estimated: 0 []
│ │ │ └─estimated: 0 [a=1]
│ │ └─
│ │   ├─estimated: 0 [b=2]
│ │   └─estimated: 0 []
│ └─
│   ├─
│   │ ├─estimated: 0 []
│   │ └─estimated: 0 []
│   └─
│     ├─estimated: 0 []
│     └─estimated: 0 []
└─
  ├─
  │ ├─
  │ │ ├─estimated: 0 []
  │ │ └─estimated: 0 []
  │ └─
  │   ├─estimated: 0 []
  │   └─estimated: 0 []
  └─
    ├─
    │ ├─estimated: 0 []
    │ └─estimated: 0 []
    └─
      ├─estimated: 0 []
      └─estimated: 0 []