forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJavaStreamsExample2.java
More file actions
53 lines (36 loc) · 835 Bytes
/
JavaStreamsExample2.java
File metadata and controls
53 lines (36 loc) · 835 Bytes
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
/* java streams with filter functionality */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.Collectors;
class Car {
private String name;
private String brand;
public Car(String a, String b){
this.name = a;
this.brand = b;
}
public String getName(){
return this.name;
}
public String getBrand(){
return this.brand;
}
}
class JavaStreamsExample2
{
public static void main (String[] args) throws java.lang.Exception
{
List<Car> list = new ArrayList();
list.add(new Car("beetle", "VW"));
list.add(new Car("mustang", "Ford"));
List<Car> filteredCars = list.stream().filter(car -> "VW".equals(car.getBrand()) ).collect(Collectors.toList());
for(Car car : filteredCars){
System.out.println(car.getName());
}
}
}
/**
Success output :
beetle
*/