Ejemplo n.º 1
0
 @SuppressWarnings("unchecked")
 @Override
 default Tuple2<Seq<T>, Seq<T>> span(Predicate<? super T> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   if (isEmpty()) {
     return Tuple.of(Stream.empty(), Stream.empty());
   } else {
     return (Tuple2<Seq<T>, Seq<T>>) traverse().span(predicate);
   }
 }
Ejemplo n.º 2
0
 static <T> Stream<T> levelOrder(Tree<T> tree) {
   Stream<T> result = Stream.empty();
   final java.util.Queue<Tree<T>> queue = new java.util.LinkedList<>();
   queue.add(tree);
   while (!queue.isEmpty()) {
     final Tree<T> next = queue.remove();
     result = result.prepend(next.getValue());
     queue.addAll(next.getChildren().toJavaList());
   }
   return result.reverse();
 }
Ejemplo n.º 3
0
 static <T> Stream<T> inOrder(Tree<T> tree) {
   if (tree.isLeaf()) {
     return Stream.of(tree.getValue());
   } else {
     final List<Node<T>> children = tree.getChildren();
     return children
         .tail()
         .foldLeft(Stream.<T>empty(), (acc, child) -> acc.appendAll(inOrder(child)))
         .prepend(tree.getValue())
         .prependAll(inOrder(children.head()));
   }
 }
Ejemplo n.º 4
0
  public static void main(String... args) {

    /*
    -------------------------------------------------------------------------
    From obj to ...
    -------------------------------------------------------------------------
    */
    Stream<String> streamOfStrings = Stream.of("un", "deux", "trois");
    Function<String, StringBuilder> function = StringBuilder::new;
    streamOfStrings.map(function) /*.forEach(System.out::println)*/;
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToIntFunction<String> toIntFunction = String::length;
    IntStream streamOfInts = streamOfStrings.mapToInt(toIntFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToDoubleFunction<String> toDoubleFunction = String::length;
    DoubleStream streamOfDoubles = streamOfStrings.mapToDouble(toDoubleFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToLongFunction<String> toLongFunction = String::length;
    LongStream streamOfLongs = streamOfStrings.mapToLong(toLongFunction);

    /*
    -------------------------------------------------------------------------
    From int to ...
    -------------------------------------------------------------------------
    */
    IntUnaryOperator plusplus = i -> i++;
    IntStream.of(1, 2, 3).map(plusplus)
    /*.forEach(x->System.out.println(x))*/ ;
    IntFunction<String> intFunction = i -> "" + i;
    IntStream.of(1, 2, 3).mapToObj(intFunction)
    /*.forEach(System.out::println)*/ ;
    IntToDoubleFunction itdf = i -> i;
    IntStream.of(1, 2, 3).mapToDouble(itdf)
    /*.forEach(System.out::println)*/ ;
    IntToLongFunction itlf = i -> i;
    IntStream.of(1, 2, 3).mapToLong(itlf)
    /*.forEach(System.out::println)*/ ;

    /*
    -------------------------------------------------------------------------
    From long to ...
    -------------------------------------------------------------------------
    */
    LongUnaryOperator times = l -> l * l;
    LongStream.of(1L, 2L, 3L).map(times)
    /*.forEach(System.out::println)*/ ;
    LongFunction<String> lf = l -> "toto";
    LongStream.of(1L, 2L, 3L).mapToObj(lf);

    /*
    -------------------------------------------------------------------------
    From double to ...
    -------------------------------------------------------------------------
    */
    DoubleToIntFunction dtif = d -> (int) d;
    DoubleStream.of(1.3, 1.5, 1.6).mapToInt(dtif).forEach(System.out::println);
  }
Ejemplo n.º 5
0
  public static void main(String[] args) {

    Map<Integer, HashSet<String>> withSet =
        Stream.of("jon", "andrew", "harvey", "bil")
            .collect(Collectors.groupingBy(String::length, HashSet::new));
    out.println(withSet);
  }
Ejemplo n.º 6
0
 @Override
 default Seq<T> takeRight(int n) {
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().takeRight(n);
   }
 }
Ejemplo n.º 7
0
 @Override
 default Seq<T> dropRight(int n) {
   if (n >= length()) {
     return Stream.empty();
   } else {
     return traverse().dropRight(n);
   }
 }
Ejemplo n.º 8
0
 @Override
 default Seq<T> filter(Predicate<? super T> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().filter(predicate);
   }
 }
Ejemplo n.º 9
0
 @Override
 default <U> Seq<T> distinctBy(Function<? super T, ? extends U> keyExtractor) {
   Objects.requireNonNull(keyExtractor, "keyExtractor is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().distinctBy(keyExtractor);
   }
 }
Ejemplo n.º 10
0
 @Override
 default Seq<T> distinctBy(Comparator<? super T> comparator) {
   Objects.requireNonNull(comparator, "comparator is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().distinctBy(comparator);
   }
 }
Ejemplo n.º 11
0
 /**
  * Traverses the Tree in a specific order.
  *
  * @param order the tree traversal order
  * @return A List containing all elements of this tree, which is List if this tree is empty.
  * @throws java.lang.NullPointerException if order is null
  */
 default Seq<T> traverse(Order order) {
   Objects.requireNonNull(order, "order is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     switch (order) {
       case PRE_ORDER:
         return Traversal.preOrder(this);
       case IN_ORDER:
         return Traversal.inOrder(this);
       case POST_ORDER:
         return Traversal.postOrder(this);
       case LEVEL_ORDER:
         return Traversal.levelOrder(this);
       default:
         throw new IllegalStateException("Unknown order: " + order.name());
     }
   }
 }
Ejemplo n.º 12
0
 static <T> Stream<T> postOrder(Tree<T> tree) {
   return tree.getChildren()
       .foldLeft(Stream.<T>empty(), (acc, child) -> acc.appendAll(postOrder(child)))
       .append(tree.getValue());
 }
Ejemplo n.º 13
0
 static <T> Stream<T> preOrder(Tree<T> tree) {
   return tree.getChildren()
       .foldLeft(Stream.of(tree.getValue()), (acc, child) -> acc.appendAll(preOrder(child)));
 }