Java 8?????????????Stream API???????
???????????? ???????[ 2015/8/28 10:44:35 ] ??????????????????? ???????
?????????“Java 8????????”????????У??????????????????????????????????????????????????δ???????????????????????????????????
??????????????“??????????????????????Lambda???????????”?У??????????????????????lambda???????????????????????????predicate????????????????????????????????????????????????
??????????????Github???????? ????????
?????????б?
????????????????????
?????????????鴴??????
??????????е????
????1. ???????????????
??????飺
????Java??????????List??Map????Arraylist??HashMap??????????????????????????????????????????????? ????????????Java SE 8?У???????????????API???????????????????????????????????????????????????
??????ι?????
???????????????μ????????????????????壬??????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????? Java8Features ?? com.tm.java8.features.stream.traversing ???μ??????????????????SequentialStream ???У?Java SE 8 ????????????????????????????????
????List<person> people = new ArrayList<>();
????people.add(new Person("Mohamed"?? 69));
????people.add(new Person("Doaa"?? 25));
????people.add(new Person("Malik"?? 6));
????Predicate<person> pred = (p) -> p.getAge() > 65;
????displayPeople(people?? pred);
????...........
????private static void displayPeople(List<person> people?? Predicate<person> pred) {
????System.out.println("Selected:");
????people.forEach(p -> {
????if (pred.test(p)) {
????System.out.println(p.getName());
????}
????});
????}
???????????????У??????????????????????????????????δ???????е????????????????????????????δ????У???????? pepole ???????б??????????List???????????? Person ?????????????????? Predicate ????????????????????????????? people ?????????? displayPeople() ??????48??52??????????ü???????????????е??????????δ???????????μ?????
????Selected:
????Mohamed
??????????????????????????δ?????????????????δ???????????????????£???????ü?????? people????????????? stream() ?????????stream?????????????????????????????????????????????????????????????????????μ?????????? Person ?????????????????????????????????
????System.out.println("Selected:");
????// people.forEach(p -> {
????// if (pred.test(p)) {
????// System.out.println(p.getName());
????// }
????// });
????people.stream().forEach(p -> System.out.println(p.getName()));
????}
??????????????? stream() ??????????????????????????????????????Щ???????????????? forEach ???????÷?????????Lamda??????????????д????????Lamda???????б??е????????????????????????????????????Lambda ???????????????????????????system output???????????????????沢??????δ???????????????????й??????????????б???????????
????Selected:
????Mohamed
????Doaa
????Malik
????????????????????????????????????? predicate ???????????? for each ???????????????????ò???????? predicate ?? test ???????????????????????????????? filter ????????÷?????????? predicate ???????е? predicate ????????? test ???????????????????????????÷?????????????????????????????.forEach()?????????????У???????м?????У???????? filter ??????
????people.stream()
????.filter(pred)
????.forEach(p -> System.out.println(p.getName()));
????filter??????????? predicate ?????????????? predicate ?????????filtr ????????????????????????????????????????????forEach()???????????????δ????????????????????????????????????????????? ??????????????????顣????? Java SE 8 API ??????doc????ɡ?
????Selected:
????Mohamed
????????????????????????????????????????????顣???????????????????????????????′???????????????????????Java SE 8 ????????????????? CPU ?????????????Java ?????????????Э????? CPU ?????С??????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????ParallelStream??????? ?μ?????????汾?У???????????????????????Щ?????????????????????????????????????????????ü????е? parallelStream()????????????????????????????????????????
????private static void displayPeople(List<person> people?? Predicate<person> pred) {
????System.out.println("Selected:");
????people.parallelStream()
????.filter(pred)
????.forEach(p -> System.out.println(p.getName()));
????}
??????????δ???????????????μ???????????????????
????Selected:
????Mohamed
??????????????????????????ε??? stream() ??????????? stream ?????????????? parallel() ???????????????????????????????????????е????????????????????????????????????????????????????????????????????????????????? ????????????????????????
????people.stream()
????.parallel()
????.filter(pred)
????.forEach(p -> System.out.println(p.getName()));
???????
????????????????????漲?????????????2???????????????????????????????С?????????????????????????????????е?? CPU ??????????????????????????ú???????????????????????????????????????????????????????????????????
????2???????????鴴????
???????
????Java SE 8’s stream API ???????????????????????????Щ??????????????е?????????????б??????????????????????????鴴??????
??????ι?????
?????? Java8Features ????е? eg.com.tm.java8.features.stream.creating ???£??????????????ArrayToStream???????????? main ?????У??????????????????????????顣?????????Person?????????????
????public static void main(String args[]) {
????Person[] people = {
????new Person("Mohamed"?? 69)??
????new Person("Doaa"?? 25)??
????new Person("Malik"?? 6)};
????for (int i = 0; i < people.length; i++) {
????System.out.println(people[i].getInfo());
????}
????}
?????????????г???????? setters ?? getters ????????? getInfo() ???????÷?????????????????????
????public String getInfo() {
????return name + " (" + age + ")";
????}
???????????????????????????????飬?????????????????????????б??????????б??????????????????????????????????????鴴??????????????????????????????????д?????????????????????????棬???????????????????
????Stream ?? java.util.stream ?μ??????????????? Ctrl+Space ????????????????????????????????????????????????????????Person???????????????????????μ????????μ???????????? stream?????е????????Сд????????????????????????????????????? of() ??????????÷???????????????汾??
??????????????????????????????????????????????????????????????????????? people ?????飬????????????????????顣Stream.of() ??????????????飬????????????????С?????????????? lambda ??????????????????????????????????????????? for each ??????????????? lambda ????????????? person ????? lambda ????????????????? person ????????????????????????? getInfo() ????????????
????Person[] people = {
????new Person("Mohamed"?? 69)??
????new Person("Doaa"?? 25)??
????new Person("Malik"?? 6)};
????// for (int i = 0; i < people.length; i++) {
????// System.out.println(people[i].getInfo());
????// }
????Stream<Person> stream = Stream.of(people);
????stream.forEach(p -> System.out.println(p.getInfo()));
???????沢??????δ?????????????????????????????????????????μ????????????????? Stream.of() ??????
????Mohamed (69)
????Doaa (25)
????Malik (6)
?????????????????????????????????????????????????????????????β???? Stream.of() ???????????????? Arrays ????????λ?? java.util ????????????????????????? stream ??????????stream ??????????????????????飬?????????????????????
????// Stream<person> stream = Stream.of(people);
????Stream<person> stream = Arrays.stream(people);
????stream.forEach(p -> System.out.println(p.getInfo()));
???????沢???????????????????????????????????μ??
????Mohamed (69)
????Doaa (25)
????Malik (6)
????????
??????????????? Stream.of() ???? Arrays.stream()?????????????????????????????????????????????????????????????????????????????? lambda ??????????????????????????
????3??????????
???????
?????????????????????????????????????????????????????????????????е???????????????????????????????????Щ?????????ū????????????????????
??????ι?????
????????? Java8Features ????? eg.com.tm.java8.features.stream.aggregating ??????????????????????? ParallelStreams ?????????? main ?????У?????????????????????????????б????????????????б????????10000???????????35??36?У?????????????????????? for each ??????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11