ListString names Arrays as Listyael dvir sivan 2

  • Slides: 25
Download presentation

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); 2 ( מחלקה אנונימית )עם שירות

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); 2 ( מחלקה אנונימית )עם שירות יחיד Collections. sort(names, new Comparator<String>() { @Override public int compare(String a, String b) { return a. compare. To(b); } } ); System. out. println(names);

3 List<String> names = Arrays. as. List("yael", "dvir", "sivan"); Collections. sort(names, new Comparator<String>() {

3 List<String> names = Arrays. as. List("yael", "dvir", "sivan"); Collections. sort(names, new Comparator<String>() { @Override public int compare(String a, String b) { return a. compare. To(b); } } למבדה ); ביטוי (λ ) Collections. sort(names, (String a, String b) -> { return a. compare. To(b); } ); System. out. println(names);

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); 4 Collections. sort(names, new Comparator<String>() {.

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); 4 Collections. sort(names, new Comparator<String>() {. . . } ); Collections. sort(names, (String a, String b) -> { return a. compare. To(b); } ); Collections. sort(names, (a, b) -> { return a. compare. To(b); } ); הסקת טיפוסי הארגומנטים ותחביר מקוצר לשירותים של שורה אחת Collections. sort(names, (a, b) -> a. compare. To(b) );

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); names. for. Each( s -> System.

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); names. for. Each( s -> System. out. println(s) ); names. stream(). map( s -> s. to. Upper. Case() ). for. Each( s -> System. out. println(s) ); names. stream(). map( String: : to. Upper. Case ). for. Each( s -> System. out. println(s) ); names. stream(). map( s-> (int) s. char. At(0) ). for. Each( s -> System. out. println(s) ); 6

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); 7 names. for. Each( s ->

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); 7 names. for. Each( s -> System. out. println(s) ); names. stream(). for. Each( s -> System. out. println(s) ); stream names. stream(). map( s -> s. to. Upper. Case() ). for. Each( s -> System. out. println(s) ); מחזיר כארגומנט names. stream(). map( String: : to. Upper. Case ). for. Each( s -> System. out. println(s) ); שירות טיפוס names. stream(). map( s-> (int) s. char. At(0) ). for. Each( s -> System. out. println(s) ); המרה של - איברי ה stream

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); names. stream(). filter( s -> s.

List<String> names = Arrays. as. List("yael", "dvir", "sivan"); names. stream(). filter( s -> s. contains("a")). for. Each( s -> System. out. println(s) ); names. stream(). filter( s -> s. contains("a")). sorted(). for. Each( s -> System. out. println(s) ); כי היא stream היא פעולה לא שגרתית על sorted. צריכה לאסוף את כל איבריו 9

10 (! יצירת זרם )אינסופי public class Natural. Numbers implements Supplier<Integer> { private int

10 (! יצירת זרם )אינסופי public class Natural. Numbers implements Supplier<Integer> { private int i = 0; public Integer get() { return ++i; } } public static void main(String[] args) { Stream<Integer> s = Stream. generate(new Natural. Numbers()); s. limit(5). map( x -> x*x ). for. Each( System. out: : println ); }

11 שימוש נוסף בכל איבר public class Natural. Numbers implements Supplier<Integer> { private int

11 שימוש נוסף בכל איבר public class Natural. Numbers implements Supplier<Integer> { private int i = 0; public Integer get() { return ++i; } } public static void main(String[] args) { Stream<Integer> s = Stream. generate(new Natural. Numbers()); s. limit(5). peek( System. out: : println ). for. Each( System. out: : println ); } ? מה יודפס

13 תרגיל public class Natural. Numbers implements Supplier<Integer> { private int i = 0;

13 תרגיל public class Natural. Numbers implements Supplier<Integer> { private int i = 0; public Integer get() { return ++i; } } public static void main(String[] args) { Stream<Integer> s = Stream. generate(new Natural. Numbers()); s. limit(5). peek( System. out: : println ) ; } ? (for. Each מה יודפס עכשיו )בלי

14 אין משמעות בלי סופניות public class Natural. Numbers implements Supplier<Integer> { private int

14 אין משמעות בלי סופניות public class Natural. Numbers implements Supplier<Integer> { private int i = 0; public Integer get() { return ++i; } } public static void main(String[] args) { Stream<Integer> s = Stream. generate(new Natural. Numbers()); s. limit(5). peek( System. out: : println ) ; } כלום! האיברים לא נצרכים ולכן לא מיוצרים בכלל

15 תמצות מידע public class Natural. Numbers implements Supplier<Integer> { private int i =

15 תמצות מידע public class Natural. Numbers implements Supplier<Integer> { private int i = 0; public Integer get() { return ++i; } } public static void main(String[] args) { Stream<Integer> s = Stream. generate(new Natural. Numbers()); Optional<T> מחזיר s. limit(5). reduce( (x, y) -> x+y ). if. Present( System. out: : println ); }

16 איסוף : מזרם לקבוצה List<Integer> l = s. limit(5). collect(Collectors. to. List());

16 איסוף : מזרם לקבוצה List<Integer> l = s. limit(5). collect(Collectors. to. List());

22 ( איסוף מתקדם )הכנות class Person { private String name; private int age;

22 ( איסוף מתקדם )הכנות class Person { private String name; private int age; Person(String name, int age) { this. name = name; this. age = age; } @Override public String to. String() { return name; } } Benjamin Winterberg דוגמה של

23 איסוף מתקדם List<Person> persons = Arrays. as. List( new Person("Max", 18), new Person("Peter",

23 איסוף מתקדם List<Person> persons = Arrays. as. List( new Person("Max", 18), new Person("Peter", 23), new Person("Pamela", 23), new Person("David", 12)); Map<Integer, List<Person>> persons. By. Age = persons. stream(). collect(Collectors. grouping. By(p -> p. age)); persons. By. Age. for. Each( (age, p) -> System. out. format("age %s: %sn", age, p) );

24 2 איסוף מתקדם Double average. Age = persons. stream(). collect(Collectors. averaging. Int(p ->

24 2 איסוף מתקדם Double average. Age = persons. stream(). collect(Collectors. averaging. Int(p -> p. age)); (to. Map) • ויש עוד אספנים מתקדמים