Skip to content

Commit c1fbc73

Browse files
committed
Add AgeGreaterThanThirty class to filter and print names of persons older than 20
1 parent d28ed31 commit c1fbc73

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class AgeGreaterThanThirty {
7+
public static void main(String[] args) {
8+
List<Person> personList = Arrays.asList(new Person("Alice", Arrays.asList("Red", "Yellow"), 49),
9+
new Person("Tom", Arrays.asList("Blue", "Orange", "Red"), 22),
10+
new Person("Rohit", Arrays.asList("Blue", "Orange", "Red"), 21),
11+
new Person("Bob", Arrays.asList("Red", "Yellow", "Black"), 19));
12+
List<String> names = personList.stream().filter(x -> x.getAge() > 20).map(Person::getName).toList();
13+
names.stream().distinct().sorted().forEach(System.out::println);
14+
}
15+
}

src/main/java/com/java8/FlatMapExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
public class FlatMapExample {
77
public static void main(String[] args) {
8-
List<Person> personList = Arrays.asList(new Person("Alice", Arrays.asList("Red", "Yellow")),
9-
new Person("Tom", Arrays.asList("Blue", "Orange", "Red")),
10-
new Person("Bob", Arrays.asList("Red", "Yellow", "Black")));
8+
List<Person> personList = Arrays.asList(new Person("Alice", Arrays.asList("Red", "Yellow"), 33),
9+
new Person("Tom", Arrays.asList("Blue", "Orange", "Red"), 22),
10+
new Person("Bob", Arrays.asList("Red", "Yellow", "Black"), 19));
1111
List<List<String>> colorList = personList.stream().map(Person::getColor).toList();
1212
System.out.println(colorList);
13-
List<String> colors = personList.stream().flatMap(x->x.getColor().stream()).toList();
13+
List<String> colors = personList.stream().flatMap(x -> x.getColor().stream()).toList();
1414
System.out.println(colors);
1515
}
1616
}

src/main/java/com/java8/Person.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
public class Person {
66
String name;
77
List<String> color;
8+
int age;
89

9-
public Person(String name, List<String> color) {
10+
public Person(String name, List<String> color, int age) {
1011
this.name = name;
1112
this.color = color;
13+
this.age = age;
14+
}
15+
16+
public int getAge() {
17+
return age;
18+
}
19+
20+
public void setAge(int age) {
21+
this.age = age;
1222
}
1323

1424
public String getName() {

0 commit comments

Comments
 (0)