Java學習(十二)

來源:互聯網
上載者:User

標籤:方便   find   相同   output   invoke   integer   out   重複   元素   

/**************************************************************/

/*線上程中更新圖形化介面,需要調用SwingUtilties.invokeLater。

/*具體例子如下:

/**************************************************************/

 

在Java中支援對數值進行功能性操作的流式操作。流式操作與資料流沒有任何關係,前者主要為List和Lambda運算式等,後者為input和output等。

stream的操作有兩類,第一類為中間操作,得到的結果還是流資料;第二類為末端操作,得到的資料為可輸出字元。

中間操作:

filter:排除stream中所有與條件/斷言不符合的元素

map:對stream中的元素逐個進行映射操作

*flatMap:將每個元素轉換成無或者更多的元素

*peek:對每個遇到的元素進行一些操作

distinct:排除資料流中完全相同的資料

sorted:對資料流進行一個排序操作

limit:限定資料流中前n個資料

substream:根據index來限定資料流中的前n個資料

skip:忽略符合條件/斷言的資料

mapToDouble/mapToInt/mapToLong:類型轉換

末端操作:

forEach:逐個檢索stream中的資料

toArray:將流中的資料輸入到數組中

min:求最小值

max:求最大值

count:計算流中的資料個數

anyMatch:判斷流中是否至少有一個元素匹配條件/斷言

allMatch:判斷流中是否全部元素配對條件/斷言

noneMatch:判斷流中是否沒有元素配對條件/斷言

findFirst:尋找流中的第一個元素

findAny:尋找流中的任意元素

average:求平均值

 流的操作一般分為三個步驟:得到資料流、對流進行中間操作、對流進行末端操作,即得到結果。流的類型有以下幾種:數組array、集合collection(包括list)、映射map。得到流的形式分別為Array.stream(array)、Collection.stream()。map沒有流,但提供以下幾種方法得到流:map.putlfAbsent、map.computelfPresent、map.merge。

stream還有以下幾個常有的子介面,如IntStream、DoubleSream、LongStream、stream<T>。

stream從串列運算轉換為並行運算很容易,只需要將stream()改為parallelStream(),對於多核電腦,可以大大提高運行效率。實現起來非常方便。

數組/字串轉換成資料流有三種形式:Arrays.stream(array)、Stream.of(array)、Arrays.asList(array).stream()。具體如下

 1    public void toStream() 2     { 3         Stream<Integer> streamI=Stream.of(1,2,3,4,5); 4         streamI.forEach(x->System.out.print(x+" ")); 5         System.out.println(); 6          7         String[] str={"Andy","Anny","Elizebath","Jane"}; 8         Stream<String> streamS=Stream.of(str); 9         streamS.forEach(x->System.out.print(x+" "));10         System.out.println();11         12         Arrays.stream(str).forEach(x->System.out.print(x+" "));13         System.out.println();14         15         Arrays.asList(str).forEach(x->System.out.print(x+" "));16         System.out.println();17         18         List<String> list=Arrays.asList("Hello","world","!");19         list.stream().forEach(x->System.out.print(x+" "));20         System.out.println();            21     }

 

下面例子以List類型為例,對流操作函數進行說明。

利用函數collect(Collectors.asList())將流資料進行儲存。

1         List<Integer> list=Arrays.asList(6,2,3,4,4,5,1,7,8,9);2         List<Integer> tempInt=list.stream()//取大於5的值3         .sorted()//從小到大排序4         .distinct()//去除重複值5         .limit(8L)//取前三個值6         .filter(x->x<8)7         .sorted()//按照從小到大排序8         .map(x->x*x*x)//映射為三次方值9         .collect(Collectors.toList());    

 

 1.min()/max()操作,尋找資料流中的最小/最大資料。控制輸出最大值還是最小值由使用者自由控制,與函數名無關。返回的實值型別為Optional。可以通過get()函數得到輸出結果。

1 Optional<Integer> min;2 Optional<Integer> max;3 min=tempInt.stream().min((n1,n2)->n1-n2);4 max=tempInt.stream().max((n1,n2)->n1-n2);

 

得到正確結果。但將max中改為

 max=tempInt.stream().max((n1,n2)->n2-n1);

 

則會得到最小結果。

 

2.count()操作,計算資料流中元素的個數。

1 long count=0;2 count=tempInt.stream().count();

 

 

3.allMatch(),判斷所有元素是否都滿足條件。是,則返回true

anyMatch(),判斷是否存在元素滿足條件。是,則返回true

NoneMatch(),判斷所有元素是否都滿足條件。否,則返回true

1         boolean bAll=false;2         boolean bAny=false;3         bAll=tempInt.stream().allMatch(x->x==8);4         bAny=tempInt.stream().anyMatch(x->x==8);        

 

 

4.findAny(),尋找stream中是否有元素2。若是,則返回第一個元素

findFirst(),尋找stream中第一個元素是否存在。若是,則返回第一個值

1         Optional<Integer> findFirst;2         Optional<Integer> findAny;3         findFirst=list.stream().findFirst();4         findAny=list.stream().findAny();

 

5.字串大小寫轉換

1         List<String> str=Arrays.asList("Hello","world","!");2         List<String>tempStr=str.stream()3         .map(String::toUpperCase)//字串大寫4         .map(String::toLowerCase)//字串小寫5         .collect(Collectors.toList());6 7         tempStr.stream().forEach(x->System.out.print(x+" "));8         System.out.println();

 

Java學習(十二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.