????public static void main(String args[]) {
????System.out.println("Creating list");
????List<string> strings = new ArrayList<>();
????for (int i = 0; i < 10000; i++) {
????strings.add("Item " + i);
????}
????strings.stream()
????.forEach(str -> System.out.println(str));
????}
??????????δ??????????????????????????????????????????????б??е????????μ??
????.........
????Item 9982
????Item 9983
????Item 9984
????Item 9985
????Item 9986
????Item 9987
????Item 9988
????Item 9989
????Item 9990
????Item 9991
????Item 9992
????Item 9993
????Item 9994
????Item 9995
????Item 9996
????Item 9997
????Item 9998
????Item 9999
??????????????????μ???????????????????????????????????????????????parallelStream????????????????????parallel??????
????????????????????????????????ò?????????????????????????????????????????
????strings.stream()
????.parallel()
????.forEach(str -> System.out.println(str));
??????????и?δ??????????????????????????????????б??к?????????????????????9999????????????????????????????????????????????????????????????????????????????顣
????.........
????Item 5292
????Item 5293
????Item 5294
????Item 5295
????Item 5296
????Item 5297
????Item 5298
????Item 5299
????Item 5300
????Item 5301
????Item 5302
????Item 5303
????Item 5304
????Item 5305
????Item 5306
????Item 5307
????Item 5308
????Item 5309
????Item 5310
????Item 5311
????????????????????????????????????е????п鶼????????????????????????????????????????? forEach() ??????????????????????????????л?????????????????????????????????????????????????????С???????????????????????????????? ????????????????????????????????????????????????????
????????????????????е????????μ??????????????????????????????Ч????????????????????????????????Ч?????????????????????忼????????ü?????????Щ??????????????????????????????????????????????????????????????
???????????????? main ???????????????????????????????????????10??000??????????б??????????? for each ??????????????
????public static void main(String args[]) {
????System.out.println("Creating list");
????List<string> strings = new ArrayList<>();
????for (int i = 0; i < 10000; i++) {
????strings.add("Item " + i);
????}
????strings.stream()
????.forEach(str -> System.out.println(str));
????}
??????????????У?????????????????м??????????????????????????????????????????????????????????????????ü???????ж????????????????ó???????????洢?????
???????????????????count????????ü???strings??.stream()?? .count()????????????????????????????????“count:”??????????????system??output???????
????//      strings.stream()
????//             .forEach(str -> System.out.println(str));
????long count = strings.stream().count();
????System.out.println("Count: " + count);
???????沢???и?δ?????????????????????????????????????????????ɡ?
????Creating list
????Count: 10000
??????????????????????СС??????????????0??????????????1000??000????????????????????δ??????????????
????Creating list
????Count: 1000000
?????????????ò?????????????????????????????????? parallel ??????
????//      strings.stream()
????//             .forEach(str -> System.out.println(str));
????long count = strings.stream().parallel().count();
????System.out.println("Count: " + count);
???????????????δ?????????????????????????????????????????????????????????????????????????????????????????顣??????? ??????????????????????????????????????????????????????????ò?????????????????????????????????????????????????????? ???鷳?? ?????????????????????
???????????????????????????????? SumAndAverage ????Σ???????????????? person ??????б????? person ??????в??????????????????????????????????????????????????е? person ?????????б?????????????μ????????????????????sum???????????
?????????????? pepole.stream() ???????????????????????????????????? mapToInt() ?????????????????????? Map Method??mapToDouble() ?? mapToLong()????Щ??????????????????????л??????????????????????????????????? lambda ??????????????????????????? mapToInt() ???????????????????????????
???????? Lambda ???????????????????? person ????????????? Lambda ???????? Lambda ??????p.getAge()?????????????????????????????????????int???????????????double????????????????????????????????? ????????????????????????????? sum() ?????????????????????м????? person ?????????????????????????????????????? System Output ??????????????????????“Total of ages”??????????????
????List<person> people = new ArrayList<>();
????people.add(new Person("Mohamed"?? 69));
????people.add(new Person("Doaa"?? 25));
????people.add(new Person("Malik"?? 6));
????int sum = people.stream()
????.mapToInt(p -> p.getAge())
????.sum();
????System.out.println("Total of ages " + sum);
???????沢??????????????????????????100??
????Total of ages 100
????????Щ????????????????????????????????????????????????????????0???????????????????????????????????Optional???????
??????????????????????????????????????????????? doule ??????????????????????? OptionalDouble ??????????????????? Optional Int ?? Optional Long?????????????? avg???????????????????????μ??????? people.stream()???????????????????? mapToInt()???????????????? lambda ???????????? average ??????
?????????????????OptionalDouble????????????????????????????????? isPresent() ??????????????double?????????????????? if/else ????????????????ж????????? avg.isPresent()???????????棬??? System Output ???“Average”????????????? else ????У????????“average wasn’t calculated”??
????OptionalDouble avg = people.stream()
????.mapToInt(p -> p.getAge())
????.average();
????if (avg.isPresent()) {
????System.out.println("Average: " + avg);
????} else {
????System.out.println("average wasn't calculated");
????}
???????????????????У??????????????????????????????????????????????????????????????????????????0????????????????????? double ??????????????沢??????δ???????? optional double ????????????????
????Total of ages 100
????Average: OptionalDouble[33.333333333333336]
?????????????????????????????У??????δ???????????????????? getAsDouble() ??????
????if (avg.isPresent()) {
????System.out.println("Average: " + avg.getAsDouble());
????} else {
????System.out.println("average wasn't calculated");
????}
???????????????? double ???????????????????δ????????????£?
????Total of ages 100
????Average: 33.333333333333336
????????
??????????? lambda ????????????÷??????????????????????????