concept Stream in category java

appears as: Stream, Stream
Modern Java in Action: Lambdas, streams, reactive and functional programming

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.

The key motivation for this is that you can now program in Java 8 at a higher level of abstraction, structuring your thoughts of turning a stream of this into a stream of that (similar to how you think when writing database queries) rather than one item at a time. Another advantage is that Java 8 can transparently run your pipeline of Stream operations on several CPU cores on disjoint parts of the input—this is parallelism almost for free instead of hard work using Threads. We cover the Java 8 Streams API in detail in chapters 4–7.

Figure 11.4. Comparing the flatMap methods of Stream and Optional

This section briefly introduces basic Scala features to give you a feel for simple Scala programs. We start with a slightly modified “Hello world” example written in an imperative style and a functional style. Then we look at some data structures that Scala supports—List, Set, Map, Stream, Tuple, and Option—and compare them with Java. Finally, we present traits, Scala’s replacement for Java’s interfaces, which also support inheritance of methods at object-instantiation time.

Now that you’ve seen how to create collections, you need to know what you can do with them. Collections in Scala support operations similar to those in the Java Stream API. You may recognize filter and map in the following example and as illustrated in figure 20.1:

Functional Programming in Java: How functional techniques improve your Java programs

This is an excerpt from Manning's book Functional Programming in Java: How functional techniques improve your Java programs.

In functional programming, you generally solve this kind of problem with a special structure: the lazy list, called Stream.

Listing 9.4. The Stream data type
public Tuple<A, Stream<A>> head() {
  A a = h.getOrElse(head.get());
  return h.isEmpty()
      ? new Tuple<>(a, new Cons<>(a, tail))
      : new Tuple<>(a, this);
}
Java 8 in Action: Lambdas, streams, and functional-style programming

This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.

6 Traditionally via the keyword synchronized, but many subtle bugs arise from its misplacement. Java 8’s Stream-based parallelism encourages a functional programming style where synchronized is rarely used; it focuses on partitioning the data rather than coordinating access to it.

Listing 5.4. Return a string of all traders’ names sorted alphabetically
String traderStr = transactions.stream() .map(transaction -> transaction.getTrader().getName()) #1 .distinct() #2 .sorted() #3 .reduce("", (n1, n2) -> n1 + n2); #4 #1: Extract all the names of the traders as a Stream of Strings. #2: Select only the unique names. #3: Sort the names alphabetically. #4: Combine each name one by one to form a String that concatenates all the names.
Figure 10.4. Comparing the flatMap methods of Stream and Optional

This section briefly introduces basic Scala features so you can get a feel for simple Scala programs. We start with a slightly modified “Hello world” example written in an imperative style and a functional style. We then look at some data structures that Scala supports—List, Set, Map, Stream, Tuple, and Option—and compare them to Java 8. Finally, we present traits, Scala’s replacement for Java’s interfaces, which also support inheritance of methods at object-instantiation time.

Finally, remember that in Java 8 you could ask for a pipeline to be executed in parallel by calling parallel on a Stream. Scala has a similar trick; you only need to use the method par: