- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 539字
- 2021-07-02 19:01:58
Working with intermediate operations
Following are some of the frequently used intermediate operations:
- filter(): The method filter() method creates a new stream by first evaluating each element in the stream that passes a certain criteria that matches a given predicate:
//Filter Operation
IntStream.rangeClosed(1, 10)
.filter(s -> s>4)
.forEach(p -> System.out.println(p));
- map(): A map() is an intermediate operation that produces an equal number of output values as there are input values. It is a unary operator that simply transforms the element so each time an element is passed to the map function it returns an object and hence there is a one-to-one mapping between the input values and the transformed output values. Map is usually required when we want to transform the elements of the stream, for example we want to create a stream of integers corresponding to the length of each string in the input stream. Such transformation can be achieved by using map():
int sumOfLength=streamSupplier.get().map(x ->x.toString().length()).peek(x->System.out.println(Integer.parseInt(x.toString())))
.mapToInt(x->x.intValue()).sum();
As you may notice, we have also used special map() functions, such as mapToInt(), corresponding to primitive data types such as int, long, and double. The purpose of these special map() functions is to provide some added pipeline operations, such as sum().
- flatmap(): flatMap() is a combination of two operations, the map() and flat() operations. When the data is passed to flatMap() it may return more than one value to flatten the result. A simple corollary can be drawn from the following representation:

Hence, flatMap() essentially takes an input element and transforms it in such a way that it may result in returning more than one value so that the resultant output is of the form Stream<T>:
//flatMap Example
Stream<List<String>>streamList = Stream.of(
Arrays.asList("FistList-FirstElement"),
Arrays.asList("SecondList-FirstElement", "SecondList-SecondElement"),
Arrays.asList("ThirdList-FirstElement"));
//The streamList is of the form List<String>
Stream<String>flatStream = streamList
.flatMap(strList ->strList.stream());
// But after applying flatMap operaton it translates into Strem<String>
flatStream.forEach(System.out::println);
Similar to the map() operation, even the flatMap() operation has primitive variants of flatMap() functions to specially cater to int, long, and double data types.
- Sorted: The elements of a stream can be sorted in their natural order by using the sorted() method. However, the element of the streams must be comparable or else ClassCastException is thrown:
//Natural Sorting
streamSupplier.get().sorted().forEach(System.out::println);
Apart from natural sorting, custom sorting of elements is also possible, where an overridden method of the sorted() method accepting a compartor can be used. If we want to change the natural sorting to reverse the order it can be achieved by:
//Comparing elements with reverse order
streamSupplier.get().sorted(Comparator.reverseOrder()).forEach(System.out::println);
Another way of sorting data is to use a Lambda function as an argument to the comparator:
//Sorting the element in reverse order based on their length streamSupplier.get().sorted(Comparator.comparing(x ->x.toString().length()).reversed()).forEach(System.out::println);
Sorting on multiple fields is also possible as sorting algorithms can be chained to sort data in a specific sequence:
//Sorting on multiple fields
streamSupplier.get().sorted(Comparator.comparing(x ->x.toString().length()).thenComparing(x->x.toString())).forEach(System.out::println);
- Distinct: The distinct() method returns the unique values of the elements in the stream. The comparison property can be altered by using the overridden equals method:
//Distinct filters all the multiple records having same length streamSupplier.get().mapToInt(x->x.toString().length()).distinct().forEach(System.out::println);
- Limit: In certain scenarios particularly in the case of infinite streams, the stream can be truncated using the limit() method with the appropriate value of maxSize:
//Limiting the size of the stream streamSupplier.get().limit(2).forEach(System.out::println);
- UI設(shè)計(jì)基礎(chǔ)培訓(xùn)教程
- Node.js Design Patterns
- Python從小白到大牛
- JavaScript 程序設(shè)計(jì)案例教程
- 網(wǎng)站構(gòu)建技術(shù)
- 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì):軟件核心復(fù)雜性應(yīng)對(duì)之道(修訂版)
- 軟件項(xiàng)目管理實(shí)用教程
- HoloLens與混合現(xiàn)實(shí)開(kāi)發(fā)
- SQL Server 2008 R2數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用(第3版)
- Zabbix Performance Tuning
- Everyday Data Structures
- Learning Ionic
- Learning Android Application Testing
- R的極客理想:量化投資篇
- Deep Learning for Natural Language Processing