- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 420字
- 2021-07-02 19:01:57
Generating streams
There are multiple ways in which stream objects can be created however, some of the most common ways in which stream objects can be generated are as follows:
- Using user/programmatically specified elements: Streams can be generated using the method of java.util.Stream interface:
//Creating Streams using user/programmatically specified elements
Stream<String>Userstream = Stream.of("Creating", "Streams", "from", "Specific", "elements");
//Creating Streams using array of objects
Stream<String>ArrayStream = Stream.of( new String[]{"Stream","from","an","array","of","objects"} );
- From arrays: The class java.util.Arrays has a method stream(array) that takes in arrays as an argument to create streams:
//Creating Streams from an array
String[] StringArray=new String[]{"We","can","convert","an","array","to","a","Stream","using","Arrays","as","well"};
Stream<String>StringStream=Arrays.stream(StringArray);
- From collections: The Java class java.util.Collection also has the methods stream() and parallelStream() to construct serial and parallel streams, respectively:
//Creating Streams from Collection
List<Double>myCollection = new ArrayList<>();
for(inti=0; i<10; i++){
myCollection.add(Math.random());
}
//sequential stream
Stream<Double>sequentialStream = myCollection.stream();
//parallel stream
Stream<Double>parallelStream = myCollection.parallelStream();
- From maps: HashMaps have a slightly different way for creating streams as they contain keys and values. The streams are hence applied on java.util.Map.entrySet().stream() and java.util.Map.values().stream():
//Stream from Hashmap
Map<String, Integer>mapData = new HashMap<>();
mapData.put("This", 1900);
mapData.put("is", 2000);
mapData.put("HashMap", 2100);
mapData.entrySet()
.stream()
.forEach(p -> System.out.println(p));
mapData.keySet()
.stream()
.forEach(p-> System.out.println(p));
- Primitive streams: The Stream API also supports three primitive streams IntStream, long stream, and double stream. From a performance point of view these stream APIs are more efficient, as the boxing and unboxing of elements do not take place and hence should be preferred wherever possible. The method range() generates a series where the starting number is inclusive while the last element is exclusive. On the other hand rangeClosed() generates a series where both the ends of the series are inclusive:
//primitive streams
IntStream.range(1, 4)
.forEach(p -> System.out.println(p));
LongStream.rangeClosed(1, 4)
.forEach(p -> System.out.println(p));
DoubleStream.of(1.0,2.0,3.0,4.0)
.forEach(p -> System.out.println(p));
- Infinite streams : Unlike collection, streams can be of infinite length. A stream interface provides methods generate() and iterate() to produce infinite streams. The generate() method is passed a function that always fetches the next element of the sequence:
//Infinite Streams using generate()
Stream <Double>sequentialDoubleStream = Stream.generate(Math :: random);
Stream<Integer>sequentialIntegerStream = Stream.generate(new AtomicInteger () :: getAndIncrement);
In the case of the iterate() method, an initial element seed is passed to the function, which generates the stream. The first element in such infinite streams is always the seed value that was passed in the iterate function:
//Infinite Streams using iterate()
Stream <Integer>sequentialIntegerStream1 = Stream.iterate (Integer.MIN_VALUE, i ->i++);
Stream <BigInteger>sequentialBigIntegerStream = Stream.iterate(BigInteger.ZERO, i ->i.add (BigInteger.TEN));
- Streams from files: The Java NIO package also allows us to create streams from files using the method lines(). Each line in the file is treated as a stream element; the decoding charset can also be defined while parsing the file:
//Streams from File
Stream<String>streamOfStrings = Files.lines(Paths.get("Apology_by_Plato.txt"));
Stream<String>streamWithCharset = Files.lines(Paths.get("Apology_by_Plato.txt"), Charset.forName("UTF-8"));
Streams are usually pipelined to perform a set of operations that comprises of a source (collectioxns, IO channel), zero or more intermediate operations and a terminal operation.
推薦閱讀
- ClickHouse性能之巔:從架構(gòu)設(shè)計(jì)解讀性能之謎
- 移動(dòng)UI設(shè)計(jì)(微課版)
- Java系統(tǒng)分析與架構(gòu)設(shè)計(jì)
- Python自動(dòng)化運(yùn)維快速入門(mén)(第2版)
- Go語(yǔ)言高效編程:原理、可觀(guān)測(cè)性與優(yōu)化
- 人臉識(shí)別原理及算法:動(dòng)態(tài)人臉識(shí)別系統(tǒng)研究
- YARN Essentials
- 你不知道的JavaScript(中卷)
- Apache Mahout Clustering Designs
- 平面設(shè)計(jì)經(jīng)典案例教程:CorelDRAW X6
- C++ System Programming Cookbook
- Oracle Database XE 11gR2 Jump Start Guide
- 虛擬現(xiàn)實(shí)建模與編程(SketchUp+OSG開(kāi)發(fā)技術(shù))
- Beginning C# 7 Hands-On:The Core Language
- 基于JavaScript的WebGIS開(kāi)發(fā)