-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStream API example for Practical test.txt
167 lines (125 loc) · 3.12 KB
/
Stream API example for Practical test.txt
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Question:
define a class "Pizza" with:
enum Size
{
SMALL,LARGE,MEDIUM;
}
private String name;
private int quantity;
private double price;
private Size size;
define setter and getter methods
create 5 objects of this class and stored them inside list.
using stream api:
count how many pizzas are having "LARGE" size
calculate sum of all the prices
calculate sum of all the quantities.
package mypack;
import java.util.ArrayList;
import java.util.List;
import mypack.Pizza.Size;
class Order
{
enum Size
{
SMALL,LARGE,MEDIUM;
}
private String name;
private int quantity;
private double price;
private Size size;
/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}
/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the Size
*/
public Size getSize() {
return size;
}
/**
* @param Size the Size to set
*/
public void setType(Size size) {
this.size = size;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
public class Demo
{
public static void main(String args[])
{
Pizza p1=new Pizza();
p1.setName("cheese pizza");
p1.setQuantity(70);
p1.setPrice(100);
p1.setSize(Size.LARGE);
Pizza p1=new Pizza();
p1.setName("chicken pizza");
p1.setQuantity(70);
p1.setPrice(100);
p1.setSize(Size.LARGE);
Pizza p1=new Pizza();
p1.setName("Veggie pizza");
p1.setQuantity(70);
p1.setPrice(100);
p1.setSize(Size.LARGE);
Pizza p1=new Pizza();
p1.setName("garlic pizza");
p1.setQuantity(70);
p1.setPrice(100);
p1.setSize(Size.LARGE);
Pizza p1=new Pizza();
p1.setName("hawai pizza");
p1.setQuantity(70);
p1.setPrice(100);
p1.setSize(Size.LARGE);
List<Order>mylist=new ArrayList<Order>();
mylist.add(o1);
mylist.add(o2);
mylist.add(o3);
System.out.println(mylist.stream().filter(x->x.getType().equals(Type.SELL)).count());
System.out.println(mylist.stream().mapToDouble(o ->o.getPrice()).sum());
System.out.println(mylist.stream().mapToInt(o ->o.getQuantity()).sum());
find out pizza name starts with 's'
average of quantity
pizzas costing more than 250
how many pizzas having price in between age 150 and 300
mylist.stream().filter(s->s.getName().startsWith("s")).forEach(System.out::println);
System.out.println(mylist.stream().mapToInt(t->t.getQuantity()).average().getAsDouble());
System.out.println(mylist.stream().filter(s->s.getPrice()>250).count());
System.out.println(mylist.stream().filter(p->p.getPrice()>=150 && p.getPrice()()<=300).count());
display all the pizzas in sorted order of pname
find out quantity where name is 'cheese_pizza'
mylist.stream().sorted((s1,s2)->s1.getName().compareTo(s2.getName())).forEach(System.out::println);
mylist.stream().filter(s->s.getName().equalsIgnoreCase("cheese_pizza")).mapToInt(i->i.getQuantity()).forEach(System.out::println);
}
}