- 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.
推薦閱讀
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- C#完全自學教程
- Python程序設計(第3版)
- C語言程序設計(第2版)
- 我的第一本算法書
- 看透JavaScript:原理、方法與實踐
- Building a Quadcopter with Arduino
- Linux Device Drivers Development
- 零基礎輕松學SQL Server 2016
- C語言程序設計實驗指導 (第2版)
- Mastering Android Game Development
- Python Interviews
- OpenCV 3 Blueprints
- App Inventor少兒趣味編程動手做
- 創意UI Photoshop玩轉移動UI設計