TheDeveloperBlog.com

Home | Contact Us

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

<< Back to JAVA

Java IntStream.Range Example (Get Range of Numbers)

IntStream.range. Suppose you want to operate on numbers in sequential order: 1, 2, 3. You could create an array and use a for-loop to get these numbers.
Alternatively, you can use IntStream.range with 2 arguments. The IntStream.rangeClosed method, which is almost the same, is also available.
An example. This program demonstrates IntStream.range and IntStream.rangeClosed. It creates the IntStreams and then displays them to the console.

Range: This has an exclusive end. So the second argument is not included in the IntStream that is returned.

RangeClosed: This has an inclusive (closed) end. If the second argument is 15, the 15 is included.

Java program that uses IntStream.range import java.util.Arrays; import java.util.stream.IntStream; public class Program { public static void main(String[] args) { // Use IntStream.range. IntStream range1 = IntStream.range(10, 15); System.out.println("Range(10, 15): " + Arrays.toString(range1.toArray())); // Use IntStream.rangeClosed. IntStream range2 = IntStream.rangeClosed(10, 15); System.out.println("RangeClosed(10, 15): " + Arrays.toString(range2.toArray())); } } Output Range(10, 15): [10, 11, 12, 13, 14] RangeClosed(10, 15): [10, 11, 12, 13, 14, 15]
Some notes. Range() is a useful method. We can create a Stream and then operate upon it with IntStream-based methods like filter(). This is a powerful approach to solving problems.Filter
A summary. Typically it is a better idea to create a range-based IntStream with range() and rangeClosed(). But an int array, and the Arrays.stream() method, can also be used.Stream
© 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