Skip to content

Commit f073aa8

Browse files
authored
#code
1 parent db065ff commit f073aa8

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

demo/ConsumerDemo.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.javatechie.pre.functional.demo;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Consumer;
6+
7+
public class ConsumerDemo {
8+
9+
public static void main(String[] args) {
10+
/*
11+
* Consumer<Integer> consumer = t -> System.out.println("Printing : " + t);
12+
*
13+
* consumer.accept(10);
14+
*/
15+
16+
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
17+
18+
list1.stream().forEach(t -> System.out.println("print : " + t));
19+
20+
}
21+
}

demo/PredicateDemo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javatechie.pre.functional.demo;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class PredicateDemo {
7+
8+
public static void main(String[] args) {
9+
10+
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
11+
12+
list1.stream().filter(t -> t % 2 == 0).forEach(t -> System.out.println("print Even: " + t));
13+
}
14+
}

demo/SupplierDemo.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javatechie.pre.functional.demo;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Supplier;
6+
7+
public class SupplierDemo {
8+
9+
public static void main(String[] args) {
10+
11+
12+
List<String> list1 = Arrays.asList();
13+
14+
System.out.println(list1.stream().findAny().orElseGet(() -> "Hi viewers"));
15+
}
16+
}

example/Book.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.javatechie.lambda.example;
2+
3+
public class Book {
4+
private int id;
5+
private String name;
6+
private int pages;
7+
8+
public int getId() {
9+
return id;
10+
}
11+
12+
public void setId(int id) {
13+
this.id = id;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public int getPages() {
25+
return pages;
26+
}
27+
28+
public void setPages(int pages) {
29+
this.pages = pages;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "Book [id=" + id + ", name=" + name + ", pages=" + pages + "]";
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
final int prime = 31;
40+
int result = 1;
41+
result = prime * result + id;
42+
result = prime * result + ((name == null) ? 0 : name.hashCode());
43+
result = prime * result + pages;
44+
return result;
45+
}
46+
47+
@Override
48+
public boolean equals(Object obj) {
49+
if (this == obj)
50+
return true;
51+
if (obj == null)
52+
return false;
53+
if (getClass() != obj.getClass())
54+
return false;
55+
Book other = (Book) obj;
56+
if (id != other.id)
57+
return false;
58+
if (name == null) {
59+
if (other.name != null)
60+
return false;
61+
} else if (!name.equals(other.name))
62+
return false;
63+
if (pages != other.pages)
64+
return false;
65+
return true;
66+
}
67+
68+
public Book() {
69+
super();
70+
// TODO Auto-generated constructor stub
71+
}
72+
73+
public Book(int id, String name, int pages) {
74+
super();
75+
this.id = id;
76+
this.name = name;
77+
this.pages = pages;
78+
}
79+
80+
}

example/BookDAO.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javatechie.lambda.example;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class BookDAO {
7+
8+
public List<Book> getBooks() {
9+
List<Book> books = new ArrayList<>();
10+
books.add(new Book(101, "Core Java", 400));
11+
books.add(new Book(363, "Hibernate", 180));
12+
books.add(new Book(275, "Spring", 200));
13+
books.add(new Book(893, "WebService", 300));
14+
return books;
15+
}
16+
}

example/BookService.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.javatechie.lambda.example;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
public class BookService {
7+
8+
/*
9+
* ( o1, o2) -> o2.getName().compareTo(o1.getName());
10+
*
11+
*/
12+
public List<Book> getBooksinSort() {
13+
List<Book> books = new BookDAO().getBooks();
14+
Collections.sort(books, (o1, o2) -> o1.getName().compareTo(o2.getName()));
15+
return books;
16+
}
17+
18+
public static void main(String[] args) {
19+
System.out.println(new BookService().getBooksinSort());
20+
}
21+
}
22+
23+
/*
24+
* class MyComparator implements Comparator<Book> {
25+
*
26+
* @Override public int compare(Book o1, Book o2) { return
27+
* o2.getName().compareTo(o1.getName()); }
28+
*/

0 commit comments

Comments
 (0)