上次老師跟大家分享了 cookie、session和token,今天給大家分享一下Java 8中的Stream API。
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)枝江,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220
Stream簡(jiǎn)介
1、Java 8引入了全新的Stream API。這里的Stream和I/O流不同,它更像具有Iterable的集合類,但行為和集合類又有所不同。
2、stream是對(duì)集合對(duì)象功能的增強(qiáng),它專注于對(duì)集合對(duì)象進(jìn)行各種非常便利、高效的聚合操作,或者大批量數(shù)據(jù)操作。
3、只要給出需要對(duì)其包含的元素執(zhí)行什么操作,比如 “過(guò)濾掉長(zhǎng)度大于 10 的字符串”、“獲取每個(gè)字符串的首字母”等,Stream 會(huì)隱式地在內(nèi)部進(jìn)行遍歷,做出相應(yīng)的數(shù)據(jù)轉(zhuǎn)換。
為什么要使用Stream
1、函數(shù)式編程帶來(lái)的好處尤為明顯。這種代碼更多地表達(dá)了業(yè)務(wù)邏輯的意圖,而不是它的實(shí)現(xiàn)機(jī)制。易讀的代碼也易于維護(hù)、更可靠、更不容易出錯(cuò)。
2、高端
實(shí)例數(shù)據(jù)源
public class Data { private static Listlist = null; static { PersonModel wu = new PersonModel("wu qi", 18, "男"); PersonModel zhang = new PersonModel("zhang san", 19, "男"); PersonModel wang = new PersonModel("wang si", 20, "女"); PersonModel zhao = new PersonModel("zhao wu", 20, "男"); PersonModel chen = new PersonModel("chen liu", 21, "男"); list = Arrays.asList(wu, zhang, wang, zhao, chen); } public static List getData() { return list; } }
Filter
/** * 過(guò)濾所有的男性 */ public static void fiterSex(){ Listdata = Data.getData(); //old List temp=new ArrayList<>(); for (PersonModel person:data) { if ("男".equals(person.getSex())){ temp.add(person); } } System.out.println(temp); //new List collect = data .stream() .filter(person -> "男".equals(person.getSex())) .collect(toList()); System.out.println(collect); } /** * 過(guò)濾所有的男性 并且小于20歲 */ public static void fiterSexAndAge(){ List data = Data.getData(); //old List temp=new ArrayList<>(); for (PersonModel person:data) { if ("男".equals(person.getSex())&&person.getAge()<20){ temp.add(person); } } //new 1 List collect = data .stream() .filter(person -> { if ("男".equals(person.getSex())&&person.getAge()<20){ return true; } return false; }) .collect(toList()); //new 2 List collect1 = data .stream() .filter(person -> ("男".equals(person.getSex())&&person.getAge()<20)) .collect(toList()); }
Map
/** * 取出所有的用戶名字 */ public static void getUserNameList(){ Listdata = Data.getData(); //old List list=new ArrayList<>(); for (PersonModel persion:data) { list.add(persion.getName()); } System.out.println(list); //new 1 List collect = data.stream().map(person -> person.getName()).collect(toList()); System.out.println(collect); //new 2 List collect1 = data.stream().map(PersonModel::getName).collect(toList()); System.out.println(collect1); //new 3 List collect2 = data.stream().map(person -> { System.out.println(person.getName()); return person.getName(); }).collect(toList()); }
FlatMap
public static void flatMapString() { Listdata = Data.getData(); //返回類型不一樣 List collect = data.stream() .flatMap(person -> Arrays.stream(person.getName().split(" "))).collect(toList()); List > collect1 = data.stream() .map(person -> Arrays.stream(person.getName().split(" "))).collect(toList()); //用map實(shí)現(xiàn) List collect2 = data.stream() .map(person -> person.getName().split(" ")) .flatMap(Arrays::stream).collect(toList()); //另一種方式 List collect3 = data.stream() .map(person -> person.getName().split(" ")) .flatMap(str -> Arrays.asList(str).stream()).collect(toList()); }
Reduce
public static void reduceTest(){ //累加,初始化值是 10 Integer reduce = Stream.of(1, 2, 3, 4) .reduce(10, (count, item) ->{ System.out.println("count:"+count); System.out.println("item:"+item); return count + item; } ); System.out.println(reduce); Integer reduce1 = Stream.of(1, 2, 3, 4) .reduce(0, (x, y) -> x + y); System.out.println(reduce1); String reduce2 = Stream.of("1", "2", "3") .reduce("0", (x, y) -> (x + "," + y)); System.out.println(reduce2); }
Collect
/** * toList */ public static void toListTest(){ Listdata = Data.getData(); List collect = data.stream() .map(PersonModel::getName) .collect(Collectors.toList()); } /** * toSet */ public static void toSetTest(){ List data = Data.getData(); Set collect = data.stream() .map(PersonModel::getName) .collect(Collectors.toSet()); } /** * toMap */ public static void toMapTest(){ List data = Data.getData(); Map collect = data.stream() .collect( Collectors.toMap(PersonModel::getName, PersonModel::getAge) ); data.stream() .collect(Collectors.toMap(per->per.getName(), value->{ return value+"1"; })); } /** * 指定類型 */ public static void toTreeSetTest(){ List data = Data.getData(); TreeSet collect = data.stream() .collect(Collectors.toCollection(TreeSet::new)); System.out.println(collect); } /** * 分組 */ public static void toGroupTest(){ List data = Data.getData(); Map > collect = data.stream() .collect(Collectors.groupingBy(per -> "男".equals(per.getSex()))); System.out.println(collect); } /** * 分隔 */ public static void toJoiningTest(){ List data = Data.getData(); String collect = data.stream() .map(personModel -> personModel.getName()) .collect(Collectors.joining(",", "{", "}")); System.out.println(collect); } /** * 自定義 */ public static void reduce(){ List collect = Stream.of("1", "2", "3").collect( Collectors.reducing(new ArrayList (), x -> Arrays.asList(x), (y, z) -> { y.addAll(z); return y; })); System.out.println(collect); }
Optional
public static void main(String[] args) { PersonModel personModel=new PersonModel(); //對(duì)象為空則打出 - Optional
并發(fā)
//根據(jù)數(shù)字的大小,有不同的結(jié)果 private static int size=10000000; public static void main(String[] args) { System.out.println("-----------List-----------"); testList(); System.out.println("-----------Set-----------"); testSet(); } /** * 測(cè)試list */ public static void testList(){ Listlist = new ArrayList<>(size); for (Integer i = 0; i < size; i++) { list.add(new Integer(i)); } List temp1 = new ArrayList<>(size); //老的 long start=System.currentTimeMillis(); for (Integer i: list) { temp1.add(i); } System.out.println(+System.currentTimeMillis()-start); //同步 long start1=System.currentTimeMillis(); list.stream().collect(Collectors.toList()); System.out.println(System.currentTimeMillis()-start1); //并發(fā) long start2=System.currentTimeMillis(); list.parallelStream().collect(Collectors.toList()); System.out.println(System.currentTimeMillis()-start2); } /** * 測(cè)試set */ public static void testSet(){ List list = new ArrayList<>(size); for (Integer i = 0; i < size; i++) { list.add(new Integer(i)); } Set temp1 = new HashSet<>(size); //老的 long start=System.currentTimeMillis(); for (Integer i: list) { temp1.add(i); } System.out.println(+System.currentTimeMillis()-start); //同步 long start1=System.currentTimeMillis(); list.stream().collect(Collectors.toSet()); System.out.println(System.currentTimeMillis()-start1); //并發(fā) long start2=System.currentTimeMillis(); list.parallelStream().collect(Collectors.toSet()); System.out.println(System.currentTimeMillis()-start2); }
調(diào)試
private static void peekTest() { Listdata = Data.getData(); //peek打印出遍歷的每個(gè)per data.stream().map(per->per.getName()).peek(p->{ System.out.println(p); }).collect(toList()); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。