Create Product class
package pmk.learnjava8withrealapps.lambda;
public class Product {
private String id;
private String name;
private long price;
private int quantity;
private String manufacturer;
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Product(String id, String name, long price, int quantity, String manufacturer) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
this.manufacturer = manufacturer;
}
public Product() {
}
@Override
public String toString() {
return "Id: " + this.id + "\nName: " + this.name + "\nPrice: " + this.price + "\nQuantity: " + this.quantity + "\nManufacturer: " + this.manufacturer;
}
}
Use Sort Operator in Lambda Expression
package pmk.learnjava8withrealapps.lambda;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List products = new ArrayList();
products.add(new Product("p1", "mobile 1", 1000, 2, "Manufacturer 1"));
products.add(new Product("p2", "mobile 2", 700, 4, "Manufacturer 1"));
products.add(new Product("p3", "laptop 1", 1200, 6, "Manufacturer 2"));
products.add(new Product("p4", "laptop 2", 7000, 9, "Manufacturer 2"));
products.add(new Product("p5", "laptop 3", 2300, 9, "Manufacturer 2"));
System.out.println("Ascending Order");
products.stream()
.sorted((p1, p2) -> (int) (p1.getPrice() - p2.getPrice()))
.forEach(p -> {
System.out.println(p.toString());
System.out.println("======================");
});
System.out.println("Descending Order");
products.stream()
.sorted((p1, p2) -> (int) (p2.getPrice() - p1.getPrice()))
.forEach(p -> {
System.out.println(p.toString());
System.out.println("======================");
});
}
}
Output
Ascending Order
Id: p2
Name: mobile 2
Price: 700
Quantity: 4
Manufacturer: Manufacturer 1
======================
Id: p1
Name: mobile 1
Price: 1000
Quantity: 2
Manufacturer: Manufacturer 1
======================
Id: p3
Name: laptop 1
Price: 1200
Quantity: 6
Manufacturer: Manufacturer 2
======================
Id: p5
Name: laptop 3
Price: 2300
Quantity: 9
Manufacturer: Manufacturer 2
======================
Id: p4
Name: laptop 2
Price: 7000
Quantity: 9
Manufacturer: Manufacturer 2
======================
Descending Order
Id: p4
Name: laptop 2
Price: 7000
Quantity: 9
Manufacturer: Manufacturer 2
======================
Id: p5
Name: laptop 3
Price: 2300
Quantity: 9
Manufacturer: Manufacturer 2
======================
Id: p3
Name: laptop 1
Price: 1200
Quantity: 6
Manufacturer: Manufacturer 2
======================
Id: p1
Name: mobile 1
Price: 1000
Quantity: 2
Manufacturer: Manufacturer 1
======================
Id: p2
Name: mobile 2
Price: 700
Quantity: 4
Manufacturer: Manufacturer 1
======================