±Èѧϰм¼Êõ¸üÖØÒªµÄÊÇ˼άµÄ¸Ä±ä£¨¶þ£©

¡º¸ÊËàÁúÍøÕªÒª_±Èѧϰм¼Êõ¸üÖØÒªµÄÊÇ˼άµÄ¸Ä±ä£¨¶þ£©¡»ÕâÌõÌâÄ¿ÊÇÎÒȺÀïµÄÈËÎʵģ¬Ëû×Ô¼ºÓÃstreamдÁËÒ»¸ö£¬¾õµÃдµÄ²»ºÃ£¬ÎÊÎÒÓ¦¸ÃÔõôÑùд£¬ÎÒµ±Ê±°ÑÕâµÀÌâÄ¿·¢ÔÚȺÀïÃ棬½á¹û·¢Ïִ󲿷ÖдµÃ¶¼²»Ì«ºÃ(½¨Òé´ó¼ÒÏȲ»ÒªÍùÏ¿´£¬×Ô¼º´ò¿ªideддÊÔÊÔ)¡£ÆäÖÐÒ»¸ö³£¼û´íÎóÊÇ...


ÕâÌõÌâÄ¿ÊÇÎÒȺÀïµÄÈËÎʵģ¬Ëû×Ô¼ºÓÃstreamдÁËÒ»¸ö£¬¾õµÃдµÄ²»ºÃ£¬ÎÊÎÒÓ¦¸ÃÔõôÑùд£¬ÎÒµ±Ê±°ÑÕâµÀÌâÄ¿·¢ÔÚȺÀïÃ棬½á¹û·¢Ïִ󲿷ÖдµÃ¶¼²»Ì«ºÃ(½¨Òé´ó¼ÒÏȲ»ÒªÍùÏ¿´£¬×Ô¼º´ò¿ªideддÊÔÊÔ)¡£ÆäÖÐÒ»¸ö³£¼û´íÎóÊÇstream²Ù×÷ÖÐÐÞ¸ÄÁËÊý¾Ý£¬Èç

1code//groupbrMapString, ListDataBean gourps = totalStocks.stream()br .collect(Collectors.groupingBy(e - e.getDeptId() + : + e.getType()));brbr// reduce(´íÎóµÄд·¨)brListDataBean result = gourps.values().stream()br .reduce(list - list.stream().reduce((e1, e2) - {br e2.setNum(e1.getNum() + e2.getNum());br return e2;br}).get())br// toListbr.collect(Collectors.toList());brbrSystem.out.println(result);br/code

»¹ÓÐÒ»ÖÖÊÇ£¬×Ô¼ºnewÁËÒ»¸öArrayList£¬È»ºóÔÙÁ÷²Ù×÷Ö®ÖÐÍùlistÀïÃæ¼ÓÊý¾ÝµÄ¡£ÖÁÓÚgroup2´ÎµÄ¾Í²»ÆÀÂÛÁË¡£

ÎÒÃÇѧϰº¯Êýʽ±à³ÌÀïÃ棬º¯Êýʽǿµ÷º¯Êý±ØÐëÊÇ´¿º¯Êý£¬²»ÄÜÐÞ¸ÄÊý¾Ý£¬¶øÇÒÊÇÃݵȣ¬ÔÚstreamÀïÃ棬ÈκÎÐÞ¸ÄÊý¾ÝµÄÐÐΪ¶¼ÊDz»Ó¦¸ÃµÄ£¬ÐÞ¸ÄÊý¾ÝµÄʱºòÓ¦¸Ã·µ»ØжÔÏó¡£ËùÒÔÔÚÀïÃæsetÊý¾ÝµÄ£¬»òÕßÍùlistÀïÃæÔö¼ÓÊý¾ÝµÄ£¬¶¼ÊDz»ºÃµÄд·¨!ÎÒÃÇѧϰÁ˺¯Êýʽ±à³Ì£¬¾ÍÓ¦¸Ã×ñÊØËûµÄ¹æÔòºÍ˼Ïë¡£ÉÏÃæµÄÌâÄ¿£¬ÎÒµÄд·¨ÈçÏ£º

1codeListDataBean result = totalStocks.stream()br //groupbr .collect(Collectors.groupingBy(e - e.getDeptId() + : + e.getType()))br // ·Ö×éºóµÄlist×öreducebr .values().stream().map(list - list.stream().reduce(Test::combine).get())br // ÊÕ¼¯µ½listbr .collect(Collectors.toList());brbrSystem.out.println(result);brbrbrprivate static DataBean combine(DataBean e1, DataBean e2){br DataBean e = new DataBean();brbr e.setDeptId(e1.getDeptId());br e.setType(e1.getType());br e.setNum(e1.getNum() + e2.getNum());brbr return e;br}br/code

ÎÒÃÇÔÚ¿´ÁíÍâÒ»ÖÖ³¡¾°£¬ÓÉÓÚ²»°´ÕÕº¯Êýʽ˼ά±àд´úÂ룬ÔÚ²»Í¬µÄjdk°æ±¾ÖУ¬½á¹û²»Ò»Ñù£¬¿ÉÄܵ¼ÖÂbug

1codeimportjava.lang.management.ManagementFactory;brimportjava.util.ArrayList;brimportjava.util.Collections;brimportjava.util.List;brbrclassDemo{br publicString msg =³õʼֵ;br}brbrpublicclassLambdaDemo2 {br publicstaticvoidmain(String[] args) {br System.out.println(jdk°æ±¾£º+ManagementFactory.getRuntimeMXBean().getVmVersion());br ListDemo demos =newArrayListDemo();brbr demos.add(newDemo());br demos.add(newDemo());brbr // ÕâÀïpeekÖÐÐÞ¸ÄÁ˶ÔÏ󣬲úÉúÁ˸±×÷ÓÃ(side-effects)br long count = demos.stream().peek(demo - demo.msg = peekÖÐÐÞ¸ÄÁË).count();br System.out.println(count);brbr // jdk8ÏÂÏÂÃæµÄdemoÒѾ­¸Ä±äbr // jdk10ÏÂûÓÐbr demos.stream().forEach(demo - System.out.println(demo.msg));brbr }br}br/code

ÍƼö

³±¡¤¿Æ¼¼

Á³Æ¤ºñ¡¢ÇéÉ̸ߡ¢»á½²»°£¡º¼ÖÝÄêÇáÈËÑÛÖеÄÐÂÖ°Òµ£¬¾ÓÈ»ÊÇÕâÑùµÄ¡­¡­

ÔĶÁ(32)

º¼Öݵĸ÷ÀàÖ±²¥»ú¹¹Ò²ÈçÓêºó´ºËñ£¬Ö÷²¥ÐÐÒµ³ÊÏÖ³öÄêÇữ¡¢¹æ·¶»¯ºÍ±ê×¼»¯µÄÇ÷ÊÆ£¬²»ÉÙ´óѧ±ÏÒµÉú°Ñ±ÏÒµºóµÄµÚÒ»·Ý¹¤×÷Ëø¶¨ÔÚÁË¡°Ö÷²¥¡±¡£ À´×ÔÖØÇìµÄ95ºóÕÔÕÔ£¬´óѧ¾Í¶ÁÓÚÒ½¿Æ...

³±¡¤¿Æ¼¼

áéÖÝÊÐáé½­ÇøÓªÉÌ°ì¾ÙÐС°áé½­ÇøÐÅÓÃÖÇÖÎ×ۺϹÜÀíϵͳ¡±ÉÏÏßÆô¶¯ÒÇʽ

ÔĶÁ(11)

¬ÌṩÃñÉú¡¢ÐÅÓᢽðÈÚµÈNÀàÐÅÓó¡¾°¡£Íõ¼ÌÃ÷Ö¸³ö£¬áé½­ÇøÐÅÓÃÖÇÖÎ×ۺϹÜÀíϵͳµÄ¿ª·¢ÉÏÏßÊÇáé½­ÇøÐÅÓÃÌåϵ½¨É蹤×÷È¡µÃµÄ½×¶ÎÐÔÖØÒª³É¹û£¬Ò²ÊÇáéÖÝÊС¶´òÔì¡°ÐÅÓÃʾ·¶Ö®³Ç¡±...

³±¡¤¿Æ¼¼

¿áÎÒ|»ªÎªÐû²¼Ð¹棬ÀÏÓû§Ó­À´¸£Àû£¬ÈÙÒ«Óû§Ò²Ã»±»¡°Å×Æú¡±£¿

ÔĶÁ(34)

º£Ë¼÷è÷ëµÄ³öÏָıäÁË»ªÎªµÄ·¢Õ¹Â·Ïߣ¬Í¬Ê±Ò²¸Ä±äÁËÎÒÃǶԹú²úÊÖ»úµÄÈÏÖª¡£´Ó»ªÎªMate7ʱ´ú¿ªÊ¼£¬»ªÎª¾Í×ßÏòÁË×ÔÖ÷¸ß¶ËµÄ²úƷ·Ïߣ¬Èç½ñµÄMate40ϵÁÐÒѾ­Äܹ»³É¹¦ºÍiPhone12µÈÆì½¢¶Ô...

³±¡¤¿Æ¼¼

СÃ׿Ƽ¼|»ªÎªMate50Pro¸ÅÄî»ú£º97%ÆÁÕ¼±ÈÅäºóÖÃ6¾µÍ·£¬±ÈСÃ×11Ç¿¶àÁË

ÔĶÁ(20)

ÑÓÐøÁ˷dz£¾­µäµÄÆÙ²¼ÆÁÔìÐÍ£¬Ö»²»¹ýÆÙ²¼±ß¿òÏÔµÃÉÔ΢խÁ˵㣬¶øÆÁĻȡÏûÁË´ò¿×¾µÍ·µÄͬʱ£¬»¹Äܹ»ÊµÏÖ3DÈËÁ³Ê¶±ðºÍÆÁϾµÍ·¹¦ÄÜ¡£Æäʵ¾ÍÊǽ«Ç°¾µÍ·²ØÔÚÆÁÄ»ÏÂÃ棬¶ø3DÈËÁ³Ê¶±ð...

³±¡¤¿Æ¼¼

Á³Æ¤ºñ¡¢ÇéÉ̸ߡ¢»á½²»°£¡º¼ÖÝÄêÇáÈËÑÛÖеÄÐÂÖ°Òµ£¬¾ÓÈ»ÊÇÕâÑùµÄ¡­¡­

ÔĶÁ(32)

º¼Öݵĸ÷ÀàÖ±²¥»ú¹¹Ò²ÈçÓêºó´ºËñ£¬Ö÷²¥ÐÐÒµ³ÊÏÖ³öÄêÇữ¡¢¹æ·¶»¯ºÍ±ê×¼»¯µÄÇ÷ÊÆ£¬²»ÉÙ´óѧ±ÏÒµÉú°Ñ±ÏÒµºóµÄµÚÒ»·Ý¹¤×÷Ëø¶¨ÔÚÁË¡°Ö÷²¥¡±¡£ À´×ÔÖØÇìµÄ95ºóÕÔÕÔ£¬´óѧ¾Í¶ÁÓÚÒ½¿Æ...

³±¡¤¿Æ¼¼

áéÖÝÊÐáé½­ÇøÓªÉÌ°ì¾ÙÐС°áé½­ÇøÐÅÓÃÖÇÖÎ×ۺϹÜÀíϵͳ¡±ÉÏÏßÆô¶¯ÒÇʽ

ÔĶÁ(11)

¬ÌṩÃñÉú¡¢ÐÅÓᢽðÈÚµÈNÀàÐÅÓó¡¾°¡£Íõ¼ÌÃ÷Ö¸³ö£¬áé½­ÇøÐÅÓÃÖÇÖÎ×ۺϹÜÀíϵͳµÄ¿ª·¢ÉÏÏßÊÇáé½­ÇøÐÅÓÃÌåϵ½¨É蹤×÷È¡µÃµÄ½×¶ÎÐÔÖØÒª³É¹û£¬Ò²ÊÇáéÖÝÊС¶´òÔì¡°ÐÅÓÃʾ·¶Ö®³Ç¡±...

¶¯Âþ

ÍõÕßÈÙÒ«Âþ»­¡¢ÊØÔ¼ÓÃÌ×·¶Ô×Ô¼ºµÜµÜ£¬µ«Ã»Ïëµ½µÜµÜ²»°´Ì×·À´£¿

ÔĶÁ(30)

µ«Ã»Ïëµ½µÜµÜ¸ù±¾²»°´Ì×·À´£¬Ö±½ÓÌø¹ýÁËËùÓÐÊ߲ˣ¬ÕâÈÃÊØÔ¼ºÜÊÇ¿àÄÕ¡£°ÙÀïÊØÔ¼Ó¦¸ÃÔõÑù²ÅÄܸĵôµÜµÜÌôʳµÄ»µÃ«²¡£¬ÈÃËû¶à³ÔÊ߲ˣ¬¶à³ÔË®¹ûÄØ£¿Ò»¸öÈËÖªµÀ×Ô¼ºÎªÊ²Ã´¶ø»î£¬¾Í...

³±¡¤¿Æ¼¼

ÔËÓªÉÌ|ÃÀ¹úÈ¡ÏûÍËÊУ¡±äÁ³À´µÃÌ«¿ì£¬Èý´óÔËÓªÉÌʼÁÏδ¼°£¿

ÔĶÁ(44)

¸Û¹É5ÈÕÔç¼äÐÐÇéÏÔʾ£¬ÖйúµçÐÅ¡¢ÖйúÒƶ¯¡¢ÖйúÁªÍ¨¾ùÕdz¬5%¡£´ÓÀÕÁîÍËÊе½È¡ÏûÍËÊУ¬ÖмäÒ²²»¹ýºÄ·ÑÁË5ÌìµÄʱ¼ä£¬ËûÃÇÕâ¸ö±äÁ³µÄËٶȹûÈ»ÊÇ¡°ÊÀ½çµÚÒ»¡±£¬Èý´óÔËÓªÉ̹À¼Æµ½ÏÖ...

»á¡¤Éú»î

Âþ»ÄÕ¦°ì

ÔĶÁ(28)

ÓÐʱ¿Ì·¢Ã÷¿ÉÒÔ¿´µÄҲû±£³ÖÏÂÈ¥£¬¾ÍÊDz»¸ÐÐËȤ£¬»¹ÒªµÈ´ýÕæÕý°®ºÃµÄ·¬°¡...

³±¡¤¿Æ¼¼

Áãë´Íæ¼Ò|Èý¹úÖ¾Õ½ÂÔ°æ:ÄÚÕþ½«Íê³ÉÒ»´Î10000Õ½¹¦µÄÊÆÈçÆÆÖñ¡¢ÏòËÀ¶øÉú³É¾Í

ÔĶÁ(11)

ÆäʵÕâЩ³É¾ÍÖ»ÊÇ¿´ÆðÀ´ÄÑ£¬ÎÒ¾õµÃ²ß»®¶¼ÒѾ­¸øÄãÏëºÃ°ì·¨ÁË¡£ÏÂÃæÊÇÄãÐèÒª×öµÄ×¼±¸³É¾Í£ºµÇ·åÔ켫¡¢ÓñøÈçÉñ¡¢ÊÆÈçÆÆÖñ¡¢ÏòËÀ¶øÉúÐèÒª×öµÄ×¼±¸²ÄÁÏ£º1¡¢Ð¡ºÅ1¸ö»òÕßС»ï°é1ÈË...

¶¯Âþ

´óÕ½|¡¶¸ßУ֮Éñ¡·ÉÏÑÝ¡°ÌæÉí¡±´óÕ½£¬Õⲿз¬Ô½À´Ô½ÓÐÒâ˼ÁË£¡

ÔĶÁ(19)

È»¶øÔ¤ÈȲ¿·Ö¾ÍºÜÎüÒýÈË£¬¸ü±ð˵ÕæÕýµÄ¾çÇéÁË¡£ÔÚ¶¯»­µÚ¶þ¼¯£¬ÄÐÖ÷³ÂëÀûÒòΪ˽×Ô¸ÉɬËûÈËÕ½¶·£¬±»²ÃÅдø×ßÁË£¬¹ØÓÚ³ÂëÀûµÄ´¦·ÖÔÚµÚÈý¼¯Ò²ÒѾ­¸ø³ö´ð°¸¡£ÄDZãÊÇÈóÂëÀûºÍ²Ã...

³±¡¤¿Æ¼¼

Ò»¼ÓNordn10 5gºÍ100¼´½«·¢²¼£¬ÖеͶËÊÖ»ú£¿

ÔĶÁ(30)

ÆäʵÏÖÔÚÄܹ»¿´³öÀ´µÄÒ»¼þÊ £¬ Ò»¼ÓÏÖÔÚÒ²¿ªÊ¼Öð½¥Ôö¼Ó×Ô¼ºµÄ²úÆ·ÊýÁ¿ £¬ µ±È»¾ÍÊÇ˵ÏÖÔÚ²»¹âÊÇרÃÅ×öÆì½¢ÊÖ»úÁË £¬ µ±È» £¬ ÕâÊÇÒ»¸öËãÊDZ»¶¯µÄ¸Ä±ä £¬ Ò»¼Ó¿´ÆðÀ´Òª·¢²¼ÐÂÆ·ÁË...