-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
62 lines (50 loc) · 1.91 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.zeeshan;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
public class Main {
public static void main(String[] args) {
// write your code here
Book b1 = new Book("Harry Potter","J.K.Rowling",20.25);
Book b2 = new Book("Harry Potter","J.K.Rowling",22.75);
Book b3 = new Book("Harry Potter","J.K.Rowling",18.90);
Book b4 = new Book("Harry Potter","J.K.Rowling",25.10);
Book b5 = new Book("7 Habits","Stephen Covey",10.30);
List<Book> listOfBooks = new ArrayList<Book>();
listOfBooks.add(b1);
listOfBooks.add(b2);
listOfBooks.add(b3);
listOfBooks.add(b4);
listOfBooks.add(b5);
List<Book> harryPotterBooks = new ArrayList<>();
for (Book book:
listOfBooks) {
if (book.getTitle().equals("Harry Potter")){
harryPotterBooks.add(book);
}
}
harryPotterBooks.sort(new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
if (o1.getPrice() > o2.getPrice()){
return 1;
} else if(o1.getPrice() < o2.getPrice()){
return -1;
}else return 0;
}
});
System.out.println("Printing sorted Harry Potter books by price asc");
for (Book book:
harryPotterBooks) {
System.out.println(book.toString());
}
harryPotterBooks = listOfBooks.stream()
.filter(b -> b.getTitle() == "Harry Potter")
.sorted(comparing(Book::getPrice))
.collect(Collectors.toList());
harryPotterBooks.forEach(System.out::println);
}
}