TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Stream: Arrays.stream and ArrayList stream

Explore the Stream class. Call Arrays.stream and convert an ArrayList to a Stream.
Stream. The water is still. Not a wave appears. The myths of a sea monster living beneath are false. As you turn to leave a tentacle grabs (with great force) your foot.
Stream usage. You must think fast. With the stream() method we can create a Stream from an array or ArrayList. With lambdas we can manipulate the stream.
Arrays.stream. Every second matters now. We begin with the Arrays.stream method. We must import the java.util.Arrays class. Stream() returns specially-typed streams.

IntStream: This is a special Stream of ints. It has powerful methods that act on ints in an optimized way.

DoubleStream: This is a Stream of doubles. The Arrays.stream method helpfully returns a DoubleStream.

Stream: Some types do not have specialized Streams. We use a Stream of Strings but must specify the type in brackets.

Here: We invoke the anyMatch method on the three streams. We test for a condition in each case. All calls here return true.

Lambda
Java program that uses Arrays.stream, IntStream import java.util.Arrays; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.Stream; public class Program { public static void main(String[] args) { // Use Arrays.stream to create an IntStream. int[] array = { 10, 20, 30 }; IntStream stream = Arrays.stream(array); // ... Call anyMatch on the IntStream. boolean result = stream.anyMatch(number -> number >= 25); System.out.println(result); // Create a DoubleStream. double[] array2 = { 1.2, 1.3, 1.4 }; DoubleStream stream2 = Arrays.stream(array2); // ... Test the DoubleStream. boolean result2 = stream2.anyMatch(number -> number >= 1.35); System.out.println(result2); // Create a Stream of Strings. String[] array3 = { "cat", "dog", "bird" }; Stream<String> stream3 = Arrays.stream(array3); // ... Test the strings. boolean result3 = stream3.anyMatch(value -> value.length() >= 4); System.out.println(result3); } } Output true true true
ArrayList stream. We can also get a stream from an ArrayList. The stream() method on an ArrayList instance returns a Stream object with a matching type.ArrayList

However: Stream() does not return an IntStream or DoubleStream, even when those types are present.

So: We must use the stream() directly when we get it from an ArrayList. We "cannot cast from a Stream(int) to an IntStream."

Java program that uses ArrayList, Stream import java.util.ArrayList; import java.util.stream.Stream; public class Program { public static void main(String[] args) { // Create an Integer ArrayList and add three numbers to it. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(200); list.add(3000); // Get a Stream from the ArrayList. Stream<Integer> stream = list.stream(); // Test the Stream. boolean result = stream.allMatch(value -> value >= 5); System.out.println(result); } } Output true
Sorted, forEach. A Stream can be sorted with the sorted() method. With strings, this sorts from first to last (from "A" to "Z"). With integers it goes also from low to high.

Here: We sort a String Stream (of bird names) and then with forEach display each element.

Java program that uses sorted, forEach import java.util.ArrayList; import java.util.stream.Stream; public class Program { static void display(String value) { // Called on each string. System.out.println("Value: " + value); } public static void main(String[] args) { // Create list of birds. ArrayList<String> list = new ArrayList<>(); list.add("parrot"); list.add("sparrow"); list.add("finch"); list.add("canary"); // Get the stream and call sorted on it. Stream<String> sorted = list.stream().sorted(); // ... Call display method on each element in sorted order. sorted.forEach(x -> display(x)); } } Output Value: canary Value: finch Value: parrot Value: sparrow
IntStream.Range. Suppose you want a range of numbers like 1, 2, 3. With the static method IntStream.range (and rangeClosed) we can easily generate this.Range
Random streams. A Random class returns one random number after another. With Random.ints() we get an IntStream of Random numbers. This is an infinite data source.Random: ints
A summary. With streams, we access powerful logic. We can transform, filter and reduce streams with method calls. Arrays and ArrayLists can be converted into streams.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf