Java 使用IntStream取代for loop

  • IntStream
  • IntStream的使用介紹

前言

建立初始陣列的方便性,傳統做法需要在一開始宣告陣列長度,再利用for loop 將值塞入。若再複雜一點要取1-10中的偶數值建立陣列,又更為複雜,

使用IntStream則可經過一連串操作後,再用forEach進行印出,也能很容易地將操作完畢的IntStream轉成array,請參考以下範例。

public static void main(String[] args) throws Exception {
	//建立陣列長度5 初始值10
	//tradition
	int [] arr1 = new int[5];;
	for(int i = 0; i<arr1.length;i++)
		arr1[i] = 10;

	//use IntStream
	arr1 = IntStream.rangeClosed(1, 5).map(s->10).toArray();
	  
	//建立1-10中偶數的陣列
	//tradition
	int [] arr2 = new int[10/2];
	for(int i =1;i<=10;i++) {
	    if(i% 2 ==0)
		   arr2[i/2-1] = i ;
	}
	for(int i = 0 ;i<arr2.length;i++)
		System.out.println(arr2[i]);
	
	System.out.println("----------------------");
	//use IntStream
	arr2 = IntStream.rangeClosed(1, 10).filter(s->s%2==0).toArray();
	Arrays.stream(arr2).forEach(System.out::println);  
}

產生數列

public static void main(String[] args) throws Exception {
	//iterate 需初始值,配合limit限制取出個數
	IntStream.iterate(0,s->s+2).limit(5).forEach(System.out::println);;
	
	//generate 不需初始值,配合limit限制取出個數
	IntStream.generate(()->(int) (Math.random()*100)).limit(5).forEach(System.out::println);
	IntStream.generate(()->5).limit(5).toArray();
}

reduce

reduce可以對IntStream做自訂運算需求生成為一 個值,比如以下例子

public static void main(String[] args) throws Exception {
	OptionalInt total1 = IntStream.rangeClosed(0, 5).reduce((u,v)->u+v);
	int total2 = IntStream.rangeClosed(1, 5).reduce(1,(u,v)->u*v);
	System.out.println(total1);//OptionalInt[15]
	System.out.println(total2);//120
}

更多用法

  • forEach
  • range、rangeClosed
  • filter
  • map
  • anyMatch
  • noneMatch
  • allMatch
  • distinct
  • sorted
public static void main(String[] args) throws Exception {
	//range rangeClosed
	IntStream.range(0, 10);//exclude 10
	IntStream.rangeClosed(0, 10);//include 10
	
	//filter map
	IntStream.range(0, 10).filter(s->s%2==0).forEach(System.out::println);//filter 0 2 4 6 8  
	IntStream.range(0, 10).map(s->s/2).forEach(System.out::println); //mapping 0 1 1 2 2 3 3 4 4
	
	//anyMatch allMatch noneMatch
	boolean isAnyBiggerThanTen = IntStream.rangeClosed(0, 10).anyMatch(s->s>10);//false
	boolean isAllSmallThanTen = IntStream.rangeClosed(0, 10).allMatch(s->s<10);//false
	boolean isAllNoneBiggerThanTen = IntStream.rangeClosed(0, 10).noneMatch(s->s>10);//true
	System.out.println(isAnyBiggerThanTen+" "+isAllSmallThanTen+" "+isAllNoneBiggerThanTen);

	//distinct
	IntStream.range(0, 10).map(s->s/2).distinct().forEach(System.out::println); //0 1 2 3 4

}

基本統計運算

IntStream有基本的統計運算maxminsumcountaverage,以及包含以上的物件方法summaryStatistics

public static void main(String[] args) throws Exception {
	
	IntStream.rangeClosed(0, 10);//include 10
	System.out.println(IntStream.rangeClosed(0, 10).max());//OptionalInt[10]
	System.out.println(IntStream.rangeClosed(0, 10).min());//OprionalInt[0]
	System.out.println(IntStream.rangeClosed(0, 10).sum());//55
	System.out.println(IntStream.rangeClosed(0, 10).average());//OptionalDouble[5.5]
	System.out.println(IntStream.rangeClosed(0,10).count());//11
	
	//summaryStatistic
	IntSummaryStatistics statistics = IntStream.rangeClosed(0, 10).summaryStatistics();
	System.out.println(statistics.getMax());//10
	System.out.println(statistics.getMin());//0
	System.out.println(statistics.getSum());//55
	System.out.println(statistics.getAverage());//5.0
	System.out.println(statistics.getCount());//11

}

Reference