馃憦馃憦馃憦馃憦鎮ㄥソ锛佸ぇ瀹跺ソ锛屾垜鏄€愬皬涓冦€戯紝涓€涓儹鐖卞垎浜悇绉嶆妧鏈殑鍗氫富锛侌煒嶐煒嶐煒嶁瓙銆愬鏃犳澧冦€戝垱浣滃畻鏃細姣忎竴鏉″懡浠ら兘浜茶嚜鎵ц杩囷紝姣忎竴琛屼唬鐮侀兘瀹為檯杩愯杩囷紝姣忎竴绉嶆柟娉曢兘瀹炶返杩囷紝姣忎竴绡囨枃绔犻兘鐢ㄨ壇蹇冧骇鍑恒€傗湂鉁娾湂猸愩€愬鏃犳澧冨皬涓冦€戝崥瀹腑鎵€鏈夋秹鍙婂懡浠ゅ拰浠g爜鐨勫湴鏂癸紝闄や簡鎻愪緵鍥剧墖渚涘ぇ瀹跺弬鑰冨锛屽浘鐗囦笅鏂硅繕浼氭湁绾枃鏈牸寮忕殑鍛戒护鎴栦唬鐮佷緵澶у绮樿创锛屽鍒跺苟鐩存帴鎵ц鍛戒护鎴栬繍琛屼唬鐮併€傪煠濔煠濔煠濃瓙濡傛灉浣犲鎶€鏈湁娴撳帤鐨勫叴瓒o紝娆㈣繋鍏虫敞銆愬皬涓冦€戯紝娆㈣繋澶у涓庢垜浜ゆ祦銆傪煒橉煒橉煒樷潳锔忊潳锔忊潳锔忔劅璋㈠悇浣嶆湅鍙嬫帴涓嬫潵鐨勯槄璇烩潳锔忊潳锔忊潳锔廆[TOC]涓€銆佸墠鏈熷噯澶?fontcolor=#8B00FF>1銆佸垱寤哄璞?fontcolor=#FF7F00>1.銆丼tudentpublicclassStudent{privateintid;绉佹湁瀛楃涓插悕绉帮紱绉佷汉瀛楃涓叉€у埆锛涚浜哄勾榫?publicStudent(intid,Stringname,Stringsex,intage){this.id=id;this.name=鍚嶇О锛泃his.sex=鎬у埆锛涜繖涓€傚勾榫?骞撮緞锛泒publicintgetId(){杩斿洖ID锛泒publicvoidsetId(intid){this.id=id;}publicStringgetName(){杩斿洖鍚嶇О锛泒publicvoidsetName(Stringname){this.name=name;}publicStringgetSex(){杩斿洖鎬у埆锛泒publicvoidsetSex(Stringsex){this.sex=sex;}publicintgetAge(){杩斿洖骞撮緞锛泒publicvoidsetAge(intage){this.age=age;}@OverridepublicStringtoString(){return"Student{"+"id="+id+",name='"+name+'\''+",sex='"+sex+'\''+",age="+age+'}';}}<瀛椾綋棰滆壊=#8B00FF>2.鍒濆鍖栨暟鎹?fontcolor=#FF7F00>2.1.鍒濆鍖栭泦鍚坧ublicclassStreamTest{ListstudentList=Arrays.asList(newStudent(1,"寮犱笁","鐢?,20),鏂扮敓(2,"鏉庡洓","鐢?,25),鏂扮敓(3,"鐜嬭垶","濂?,18),鏂扮敓(4,"璧靛叚","濂?,26));浜屻€丼tream鐨勫父鐢ㄦ搷浣滄柟娉?fontcolor=#8B00FF>1銆佽繃婊?fontcolor=#FF7F00>1.1銆乫ilterfilter浠庨泦鍚堜腑杩囨护涓€浜涘厓绱狅紝渚嬪querycollectionisolderthanDatacollectionof20//鏌ヨ骞撮緞澶т簬20鐨勬暟鎹泦鍚圠istlist=studentList.stream().filter(s->s.getAge()>20).collect(Collectors.toList());1.2銆乴imitlimit锛岀被浼间簬mysql涓殑limit锛岃繑鍥炴寚瀹氭暟閲忕殑鏁版嵁//鏌ヨ1鏉″勾榫勫ぇ浜?0鐨勬暟鎹甃istlist=studentList.stream().filter(s->s.getAge()>20).limit(1).collect(Collectors.toList());1.3銆乻kipskip锛岃烦杩囧厓绱狅紝杩斿洖璺宠繃鍓峮涓厓绱犵殑闆嗗悎//鏌ヨage澶т簬20鐨勬暟鎹紝鍓嶉潰涓嶆槸Listlist=studentList.stream().filter(s->s.getAge()>20).skip(1).collect(Collectors.toList());1.4,distinct,filter,pass鍏冪礌鐨刪ashCode()鍜宔quals()鍘婚櫎閲嶅鍏冪礌//鏌ヨ骞撮緞澶т簬20鐨勬暟鎹紝鍘婚噸Listlist=studentList.stream().filter(s->s.getAge()>20).distinct().collect(Collectors.toList());2.鏄犲皠2.1銆俶apmap锛屽皢闆嗗悎鍏冪礌杞崲鎴愬叾浠栧舰寮忚繑鍥烇紝鎺ュ彈涓€涓嚱鏁颁綔涓哄弬鏁帮紝鍑芥暟浣滅敤浜庢瘡涓€涓厓绱狅紝骞跺皢鍏禡ap鎴愪竴涓柊鐨勫厓绱?/鏌ヨ骞撮緞澶т簬20鐨勬暟鎹紝鍙緱鍒板悕瀛楅泦鍚圠istlist=studentList.stream()銆俧ilter(s->s.getAge()>20).map(Student::getName).collect(Collectors.toList());3.鎺掑簭3.1銆俿orted()sorted()鑷劧鎺掑簭//鏌ヨ骞撮緞澶т簬20鐨勬暟鎹紝鍙彇鍚嶅瓧锛屾帓搴廘istlist=studentList.stream().filter(s->s.getAge()>20).map(Student::getName).sorted().collect(Collectors.toList());3.2,sorted(Comparatorcom)鑷畾涔夋帓搴弒orted(Comparatorcom)鑷畾涔夋帓搴忥紝鑷畾涔夎緭鍏ユ帓搴忚鍒?/鏌ヨ骞撮緞澶т簬20鐨勬暟鎹紝鎸夊鍚嶆帓搴廘istlist=studentList.stream().filter(s->s.getAge()>20).sorted((e1,e2)->{returne1.getName().compareTo(e2.getName());}).collect(Collectors.toList());4.鏌ユ壘骞跺尮閰?fontcolor=#FF7F00>4.1.allMatchallMatchcheck鏄惁鍖归厤鎵€鏈夊厓绱?/鍒ゆ柇闆嗗悎涓殑鎵€鏈夊悕瀛楁槸鍚﹂兘绛変簬鈥滃紶涓夆€漛ooleanflag=studentList.stream().allMatch((e)->e.getName().equals("寮犱笁"));4.2銆乤nyMatchanyMatch鏄惁鍖归厤鑷冲皯涓€涓厓绱?/鍒ゆ柇闆嗗悎鏄惁鑷冲皯鏈変竴涓悕瀛楃瓑浜庘€滃紶涓夆€漛ooleanflag=studentList.stream().anyMatch((e)->e.getName().equals("寮犱笁"));4.3銆乶oneMatchnoneMatch妫€鏌ユ槸鍚︽墍鏈夊厓绱犻兘涓嶅尮閰?/鍒ゆ柇闆嗗悎涓槸鍚︽病鏈夊悕瀛楃瓑浜庘€滃紶涓夆€濈殑鍖归厤鍏冪礌booleanflag=studentList.stream().noneMatch((e)->e.getName().equals("寮犱笁"));4.4銆乫indFirstfindFirst杩斿洖绗竴涓厓绱?/杩斿洖闆嗗悎涓殑绗竴涓厓绱燨ptionalstudent=studentList.stream().findFirst();4.5,findAnyfindAny杩斿洖褰撳墠闆嗗悎涓殑浠绘剰鍏冪礌//杩斿洖闆嗗悎涓殑浠绘剰鍏冪礌Optionalstudent=studentList.stream().findAny();4.6銆乧onutconut杩斿洖娴佷腑鐨勫厓绱犳€绘暟//杩斿洖闆嗗悎鐨勪釜鏁發ongnum=studentList.stream().count();4.7,max杩斿洖娴佷腑鐨勬渶澶у€?/杩斿洖闆嗗悎涓渶鏃╃殑涓€鏉℃暟鎹甇ptionalstudent=studentList.stream().max((e1,e2)->Integer.compare(e1.getAge(),e2.getAge()));4.8,min杩斿洖娴佷腑鐨勬渶灏忓€?/杩斿洖闆嗗悎涓渶鏃╃殑涓€鏉℃暟鎹甇ptionalstudent=studentList.stream().min((e1,e2)->Integer.compare(e1.getAge(),e2.getAge()));