Java streams map example

Interface Stream

A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using Stream and IntStream :

 int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum(); 

In this example, widgets is a Collection . We create a stream of Widget objects via Collection.stream() , filter it to produce a stream containing only the red widgets, and then transform it into a stream of int values representing the weight of each red widget. Then this stream is summed to produce a total weight.

In addition to Stream , which is a stream of object references, there are primitive specializations for IntStream , LongStream , and DoubleStream , all of which are referred to as «streams» and conform to the characteristics and restrictions described here.

To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate) ), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer) ). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.

A stream implementation is permitted significant latitude in optimizing the computation of the result. For example, a stream implementation is free to elide operations (or entire stages) from a stream pipeline — and therefore elide invocation of behavioral parameters — if it can prove that it would not affect the result of the computation. This means that side-effects of behavioral parameters may not always be executed and should not be relied upon, unless otherwise specified (such as by the terminal operations forEach and forEachOrdered ). (For a specific example of such an optimization, see the API note documented on the count() operation. For more detail, see the side-effects section of the stream package documentation.)

Collections and streams, while bearing some superficial similarities, have different goals. Collections are primarily concerned with the efficient management of, and access to, their elements. By contrast, streams do not provide a means to directly access or manipulate their elements, and are instead concerned with declaratively describing their source and the computational operations which will be performed in aggregate on that source. However, if the provided stream operations do not offer the desired functionality, the BaseStream.iterator() and BaseStream.spliterator() operations can be used to perform a controlled traversal.

A stream pipeline, like the «widgets» example above, can be viewed as a query on the stream source. Unless the source was explicitly designed for concurrent modification (such as a ConcurrentHashMap ), unpredictable or erroneous behavior may result from modifying the stream source while it is being queried.

  • must be non-interfering (they do not modify the stream source); and
  • in most cases must be stateless (their result should not depend on any state that might change during execution of the stream pipeline).
Читайте также:  Class naming convention in python

Such parameters are always instances of a functional interface such as Function , and are often lambda expressions or method references. Unless otherwise specified these parameters must be non-null.

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, «forked» streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. However, since some stream operations may return their receiver rather than a new stream object, it may not be possible to detect reuse in all cases.

Streams have a BaseStream.close() method and implement AutoCloseable . Operating on a stream after it has been closed will throw IllegalStateException . Most stream instances do not actually need to be closed after use, as they are backed by collections, arrays, or generating functions, which require no special resource management. Generally, only streams whose source is an IO channel, such as those returned by Files.lines(Path) , will require closing. If a stream does require closing, it must be opened as a resource within a try-with-resources statement or similar control structure to ensure that it is closed promptly after its operations have completed.

Stream pipelines may execute either sequentially or in parallel. This execution mode is a property of the stream. Streams are created with an initial choice of sequential or parallel execution. (For example, Collection.stream() creates a sequential stream, and Collection.parallelStream() creates a parallel one.) This choice of execution mode may be modified by the BaseStream.sequential() or BaseStream.parallel() methods, and may be queried with the BaseStream.isParallel() method.

Источник

Java 8 Stream map()

Stream API — новый способ взаимодействия с данными, представляя их в виде конечного потока данных.

Один из методов Stream-ов, map принимает лямбда-выражение известное как функция (Function), которое преобразовывает Stream одного типа данных в Stream другого типа.

С ними мы сегодня и научимся работать.

2. Multiply integers

Выведем список чисел, умноженных на 2

Теперь мы можем использовать метод map() в Stream API, а так же метод forEach и ссылку на метод

В данном случае мы преобразовали Stream чисел в Stream чисел, умноженных на два, после чего вывели их на экран

3. Capitalize Strings with Stream map()

Выведем список имен, но с первой заглавной буквой

И это без проверок на null и пустую строку

В Java 8 мы можем воспользоваться методом map и новым утилитным классом StringUtils

Читайте также:  Java file merge tool

Что значительно упрощает задачу

4. Objects to Strings

Создадим класс Car

Предположим, нужно вывести все номера машин

5. Stream filter + map()

Усложним предыдущую задачу, теперь нужно вывести все не пустые номера машин, выпущенных после 2010 года

Сейчас мы можем объеденить методы filter и map()

Надеемся, что наша статья была Вам полезна. Также есть возможность записаться на наши курсы по Java в Киеве. Детальнее — у нас на сайте.

Источник

Java 8 — Stream.map() Examples

A Stream is a sequence of objects that supports many different methods that can be combined to produce the desired result.

They can be created from numerous data sources, which are most often collections but can also be I/O channels, Arrays , primitive data types etc.

It’s important to emphasize that a stream is not a data structure, since it doesn’t store any data. It’s just a data source wrapper which allows us to operate with data faster, easier and with cleaner code.

A stream also never modifies the original data structure, it just returns the result of the pipelined methods.

Types of Streams

Streams can be sequential (created with stream() ), or parallel (created with parallelStream() ). Parallel streams can operate on multiple threads, while sequential streams can’t.

Operations on streams can be either intermediate or terminal:

Intermediate operations on streams return a stream. This is why we can chain multiple intermediate operations without using semicolons. Intermediate operations include the following methods:

  • map(op) — Returns a new stream in which the provided op function is applied to each of the elements in the original stream
  • filter(cond) — Returns a new stream which only contains the elements from the original stream that satisfy the condition cond , specified by a predicate
  • sorted() — Returns the original stream, but with the elements being sorted

Terminal operations are either void or return a non-stream result. They’re called terminal because we can’t chain any more stream operations once we’ve used a terminal one, without creating a new stream from it and starting again.

Some of the terminal operations are:

  • collect() — Returns the result of the intermediate operations performed on the original stream
  • forEach() — A void method used to iterate through the stream
  • reduce() — Returns a single result produced from an entire sequence of elements in the original stream

In this tutorial, we’ll go over the map() operation and how we can use it with Streams to convert/map objects of various types.

Stream.map() Examples

Let’s take a look at a couple of examples and see what we can do with the map() operation.

Stream of Integers to Stream of Strings

Arrays.asList(1, 2, 3, 4).stream() .map(n -> "Number " + String.valueOf(n)) .forEach(n -> System.out.println(n + " as a " + n.getClass().getName())); 

Here, we’ve made a list of integers and called stream() on the list to create a new stream of data. Then, we’ve mapped each number n in the list, via the map() method, to a String. The Strings simply consist of «Number» and the String.valueOf(n) .

Читайте также:  Python params to script

So for each number in our original list, we’ll now have a «Number n» String corresponding to it.

Since map() returns a Stream again, we’ve used the forEach() method to print each element in it.

Running this code results in:

Number 1 as a java.lang.String Number 2 as a java.lang.String Number 3 as a java.lang.String Number 4 as a java.lang.String 

Note: This hasn’t altered the original list in the slightest. We’ve simply processed the data and printed the results. If we wanted to persist this change, we’d collect() the data back into a Collection object such as a List , Map , Set , etc:

List list = Arrays.asList(1, 2, 3, 4); List mappedList = list.stream() .map(n -> "Number " + String.valueOf(n)) .collect(Collectors.toList()); System.out.println(list); System.out.println(mappedList); 
[1, 2, 3, 4] [Number 1, Number 2, Number 3, Number 4] 

Stream of Strings into Stream of Integers

Now, let’s do it the other way around — convert a stream of strings into a stream of integers:

Arrays.asList("1", "2", "3", "4").stream() .map(n -> Integer.parseInt(n)) .forEach(n -> System.out.println(n)); 

As expected, this code will produce the following output:

List of Objects into List of Other Objects

Let’s say we have a class Point that represents one point in a cartesian coordinate system:

public class Point < int X; int Y; Point(int x, int y)< this.X=x; this.Y=y; > @Override public String toString() < return "(" + this.X + ", " + this.Y + ")"; > > 

Then, say we want to scale a certain shape by a factor of 2 , this means we have to take all the points we have, and double both their X and Y values. This can be done by mapping the original values to their scaled counterparts.

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Then, since we’d like to use the new coordinates, we’ll collect this data into a new list of scaled points:

List originalPoints = Arrays.asList(new Point(1, 2), new Point(3, 4), new Point(5, 6), new Point(7, 8)); System.out.println("Original vertices: " + originalPoints); List scaledPoints = originalPoints .stream() .map(n -> new Point(n.X * 2, n.Y * 2)) .collect(Collectors.toList()); System.out.println("Scaled vertices: " + scaledPoints); 

This example will produce the following output:

Original vertices: [(1, 2), (3, 4), (5, 6), (7, 8)] Scaled vertices: [(2, 4), (6, 8), (10, 12), (14, 16)] 

Conclusion

In this article, we explained what streams are in Java. We mentioned some of the basic methods used on streams, and focused specifically on the map() method and how it can be used for stream manipulation.

It is important to mention that streams are not really a mandatory part of programming, however they are more expressive and can significantly improve the readability of your code, which is why they became a common programming practice.

Источник

Оцените статью