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

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.
主站蜘蛛池模板: 翁牛特旗| 天柱县| 高尔夫| 禄丰县| 西峡县| 宁都县| 武乡县| 泸定县| 清流县| 芦溪县| 桂林市| 大宁县| 湘乡市| 巴楚县| 柏乡县| 五华县| 台东市| 鸡东县| 鄂伦春自治旗| 玛曲县| 大兴区| 修武县| 祁连县| 汉阴县| 乐亭县| 和林格尔县| 长葛市| 定安县| 海宁市| 鄂尔多斯市| 东港市| 霍林郭勒市| 临颍县| 嘉兴市| 乡宁县| 宁陕县| 井研县| 东源县| 嵊泗县| 日喀则市| 新丰县|