官术网_书友最值得收藏!

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:

  1. 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"} );
  1. 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);
  1. 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();
  1. 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));
  1. 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));
  1. 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));
  1. 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.
主站蜘蛛池模板: 青神县| 南开区| 郓城县| 威海市| 扶风县| 南和县| 葫芦岛市| 清远市| 麻栗坡县| 陵川县| 确山县| 马山县| 蕉岭县| 皮山县| 滦南县| 葫芦岛市| 策勒县| 策勒县| 澄江县| 湖南省| 寿宁县| 西乌珠穆沁旗| 吉水县| 黄平县| 霞浦县| 盐池县| 自治县| 孟津县| 苗栗县| 安达市| 宾阳县| 东乌珠穆沁旗| 延长县| 苍南县| 富宁县| 读书| 广德县| 日喀则市| 岗巴县| 卢湾区| 秭归县|