|
1 | 1 | package com.advancedjava;
|
2 | 2 |
|
3 | 3 | import com.advancedjava.*;
|
| 4 | +import java.util.ArrayList; |
4 | 5 | import java.util.HashMap;
|
5 | 6 |
|
6 |
| -class Square { |
| 7 | +public class Exx10Streams { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + ArrayList<Book> books = populateLibrary(); |
| 11 | + |
| 12 | + books.stream() |
| 13 | + .filter(book-> book.getAuthor().startsWith("B")) |
| 14 | + .forEach(System.out::println); |
7 | 15 |
|
8 |
| - int sideLength; |
9 | 16 |
|
10 |
| - public Square(int sideLength) { |
11 |
| - this.sideLength = sideLength; |
12 | 17 | }
|
13 | 18 |
|
14 |
| - public int calculateArea() { |
15 |
| - return sideLength * sideLength; |
| 19 | + private static ArrayList<Book> populateLibrary() { |
| 20 | + ArrayList<Book> list = new ArrayList(); |
| 21 | + list.add(new Book("Bodrul", "Funcitonal programming")); |
| 22 | + list.add(new Book("Bodrul", "Funcitonal programming")); |
| 23 | + list.add(new Book("Khalil", "Hello programming")); |
| 24 | + list.add(new Book("Rakib", "Funcitonal programming")); |
| 25 | + list.add(new Book("Bodrul", "Funcitonal programming")); |
| 26 | + list.add(new Book("Bodrul", "Funcitonal programming")); |
| 27 | + |
| 28 | + |
| 29 | + return list; |
16 | 30 | }
|
17 |
| -} |
18 | 31 |
|
19 |
| -@FunctionalInterface |
20 |
| -interface Shapes{ |
21 |
| - public abstract int getArea(Square person); |
22 | 32 | }
|
23 | 33 |
|
| 34 | +class Book { |
24 | 35 |
|
25 |
| -public class Exx10Streams { |
| 36 | + private String author; |
| 37 | + private String title; |
26 | 38 |
|
27 |
| - public static void main(String[] args) { |
28 |
| - Square s = new Square(4); |
29 |
| - Shapes shapes = ((square) -> { |
30 |
| - return square.calculateArea(); //To change body of generated lambdas, choose Tools | Templates. |
31 |
| - }); |
| 39 | + public Book(String author, String title) { |
| 40 | + this.author = author; |
| 41 | + this.title = title; |
| 42 | + } |
| 43 | + |
| 44 | + public String getTitle() { |
| 45 | + return title; |
| 46 | + } |
32 | 47 |
|
33 |
| - Shapes altShapes = Square::calculateArea; |
| 48 | + public void setTitle(String title) { |
| 49 | + this.title = title; |
| 50 | + } |
| 51 | + |
| 52 | + public String getAuthor() { |
| 53 | + return author; |
| 54 | + } |
34 | 55 |
|
35 |
| - System.out.println(shapes.getArea(s)); |
36 |
| - System.out.println(altShapes.getArea(s)); |
| 56 | + public void setAuthor(String author) { |
| 57 | + this.author = author; |
| 58 | + } |
37 | 59 |
|
38 |
| - |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + return "Book{" + "author=" + author + ", title=" + title + '}'; |
39 | 63 | }
|
| 64 | + |
40 | 65 |
|
41 | 66 | }
|
0 commit comments