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

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.
主站蜘蛛池模板: 揭西县| 吕梁市| 泽普县| 平远县| 乌鲁木齐县| 伊春市| 株洲市| 资阳市| 广水市| 蕉岭县| 卢氏县| 德安县| 定南县| 东至县| 青河县| 南丹县| 额济纳旗| 辽宁省| 永靖县| 焦作市| 隆林| 涪陵区| 南阳市| 洱源县| 巴彦县| 南川市| 镇坪县| 东乌珠穆沁旗| 孟津县| 南充市| 河曲县| 柳河县| 鹿邑县| 临沧市| 阜城县| 全南县| 丹阳市| 南充市| 台江县| 灯塔市| 天气|