Java中Range函數的簡單介紹
前言
在Java中,Range方法在IntStream和LongStream類中都可用。在IntStream類中,它有助于返回函數參數范圍內IntStream的順序值。在該方法中,startInclusive(inclusive)和endExclusive(exclusive)是與增量步長一起使用的兩個參數,如前所述,將包括起始值,并排除結束值。在LongStream的情況下,唯一的區(qū)別是添加了LongStream值。
Range語法
讓我們看看Java中range方法的語法。
IntStream范圍的語法
static IntStream range(int startInclusive, int endExclusive)
參數:
- IntStream:這是一個原始類型的int值元素序列。
- startInclusive:包含在范圍中的初始值。
- endExclusive:在范圍內排除的最后一個值或上限。
返回值:
該方法返回范圍中提到的int元素的連續(xù)int流作為參數。
LongStream范圍的語法
static LongStream range(int startInclusive, int endExclusive)
參數:
- LongStream:這是一個基元類型的長值元素序列。
- startInclusive:包含在范圍中的初始值。
- endExclusive:在范圍內排除的最后一個值或上限。
返回值:
該方法返回范圍中提到的長元素的連續(xù)長流作為參數。
Range函數在Java中是如何工作的?
首先,讓我們看看IntStream范圍在Java中是如何工作的。與Java中的其他類類似,這個類也需要一個必須首先導入的包。也就是說,為了使用IntStream類,導入包java.util.stream.IntStream。導入后,創(chuàng)建一個IntStream,以便可以向其中添加元素。創(chuàng)建流后,使用方法range()添加元素。在執(zhí)行代碼時,將通過在參數中提到的范圍內的一個增量步驟返回一個序列有序IntStream。
要打印每個元素,請使用如下所示的方法。
intStream.forEach(System.out::println);
對于LongStream,首先導入包java.util.stream.LongStream。與IntStream的功能類似,一旦導入包,就創(chuàng)建一個LongStream,以便可以向其中添加元素。創(chuàng)建流后,使用方法range()添加元素。在執(zhí)行代碼時,將通過在參數中提到的范圍內的一個增量步驟返回序列有序的長流。
用于使用如下所示的方法打印每個元素。
LongStream.forEach(System.out::println);
在for循環(huán)的幫助下,可以按順序生成遞增元素的等效打印序列,
如下所示:
for (inti = startInclusive; i<endExclusive ; i++)
{... . . . }Java中的Range示例
以下是提到的示例:
示例#1
Java程序實現IntStream Range函數。
代碼:
// IntStream range implementation using Java
import java.util.*;
//import the package for IntStream
import java.util.stream.IntStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create an IntStream
IntStream st = IntStream.range(32, 45);
// Display the elements in the range mentioned as 32 and 45 where 32 is included and 45 is excluded
System.out.println("The elements are:");
st.forEach(System.out::println);
} }輸出:

首先,導入包java.util.stream.IntStream。然后,創(chuàng)建一個IntStream st,用于向其中添加元素。在創(chuàng)建流的過程中,使用方法range(32,45)添加元素,其中包括32個元素,排除45個元素。在執(zhí)行代碼時,將通過一個增量步驟從32到44返回一個有序的IntStream,如示例輸出中所示。
示例#2
Java程序實現LongStream range范圍函數。
代碼:
// LongStream range implementation using Java
import java.util.*;
//import the package for LongStream
import java.util.stream.LongStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create a LongStream
LongStream st = LongStream.range(1000001L, 1000010L);
// Display the elements in the range mentioned as 1000001L and 1000010L where 1000001L is included and 1000010L is excluded
System.out.println("The elements are:");
st.forEach(System.out::println);
} }輸出:

與上述程序類似,導入包java.util.stream.LongStream。然后,創(chuàng)建一個具有方法range(100001L、100010L)的LongStreamst,用于向其添加元素。在執(zhí)行代碼時,將通過一個增量步驟從100001L返回到100010L,如示例輸出所示。
示例#3
Java程序,用于組合實現LongStream和IntStream range范圍函數。
代碼:
import java.util.*;
//import the package for IntStream
import java.util.stream.IntStream;
//import the package for LongStream
import java.util.stream.LongStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create an IntStream
IntStream str = IntStream.range(32, 45);
// Display the elements in the range mentioned as 32 and 45 where 32 is included and 45 is excluded
System.out.println("The IntStream elements are:");
str.forEach(System.out::println);
// Create a LongStream
LongStream st = LongStream.range(1000001L, 1000010L);
// Display the elements in the range mentioned as 1000001L and 1000010L where 1000001L is included and 1000010L is excluded
System.out.println("The LongStream elements are:");
st.forEach(System.out::println);
} }輸出:

導入包java.util.stream.IntStream 和 java.util.stream.LongStream。然后,創(chuàng)建IntStreamstr和LongStreamst以向其中添加元素。在創(chuàng)建流期間,使用方法range(32,45)在IntStream中添加元素,其中包括32,排除45。同樣,使用方法range(100001L、100010L)在LongStream中添加元素。在執(zhí)行代碼時,序列有序IntStream將從32返回到44,LongStream將通過增量步驟1從100001L返回到100010L。
到此這篇關于Java中Range函數的簡單介紹的文章就介紹到這了,更多相關Java Range函數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java concurrency之鎖_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java concurrency之鎖的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Apache?Commons?Config管理配置文件核心功能使用
這篇文章主要為大家介紹了Apache?Commons?Config管理和使用配置文件核心深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
springboot服務正常啟動之后,訪問服務url無響應問題及解決
這篇文章主要介紹了springboot服務正常啟動之后,訪問服務url無響應問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Java編程實現基于TCP協(xié)議的Socket聊天室示例
這篇文章主要介紹了Java編程實現基于TCP協(xié)議的Socket聊天室,結合實例形式詳細分析了java基于TCP協(xié)議的Socket聊天室客戶端與服務器端相關實現與使用技巧,需要的朋友可以參考下2018-01-01
SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
這篇文章主要介紹了SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
詳解五種方式讓你在java中讀取properties文件內容不再是難題
這篇文章主要介紹了詳解五種方式讓你在java中讀取properties文件內容不再是難題 ,非常具有實用價值,需要的朋友可以參考下。2016-12-12

