Searching...
Monday, September 10, 2018

Java 10 ArrayList with local variable Var and Different ways to iterate

September 10, 2018

Java10ArrayListWithLocalVar.java
 package com.java9r;

import java.util.ArrayList;

public class Java10ArrayListWithLocalVar {

public static void main(String[] args) {

var products = new ArrayList<Product>();
var product1 = new Product();
product1.setId(100);
product1.setName("Product 1");
product1.setPrice(55.2);
product1.setQuantity(20);
products.add(product1);

var product2 = new Product();
product2.setId(101);
product2.setName("Product 2");
product2.setPrice(54.2);
product2.setQuantity(10);
products.add(product2);

System.out.println("Iterate list using Enanced for loop");
System.out.println("======================================");
for (var product : products) {
System.out.println("Id: " + product.getId());
System.out.println("Name: " + product.getName());
System.out.println("Price: " + product.getPrice());
System.out.println("Quantity: " + product.getQuantity());
System.out.println("======================================");
}

System.out.println("Iterate list using Stream API");
System.out.println("======================================");
products.forEach((product) -> {
System.out.println("Id: " + product.getId());
System.out.println("Name: " + product.getName());
System.out.println("Price: " + product.getPrice());
System.out.println("Quantity: " + product.getQuantity());
System.out.println("======================================");
});

System.out.println("Iterate list using for loop ");
System.out.println("======================================");
for (var i = 0; i < products.size(); i++) {

System.out.println("Id: " + products.get(i).getId());
System.out.println("Name: " + products.get(i).getName());
System.out.println("Price: " + products.get(i).getPrice());
System.out.println("Quantity: " + products.get(i).getQuantity());
System.out.println("======================================");
}

System.out.println("Iterate list using while loop ");
System.out.println("======================================");
var i = 0;
while (i < products.size()) {

System.out.println("Id: " + products.get(i).getId());
System.out.println("Name: " + products.get(i).getName());
System.out.println("Price: " + products.get(i).getPrice());
System.out.println("Quantity: " + products.get(i).getQuantity());
System.out.println("======================================");
i++;
}

}

}


Product.java
 package com.java9r;

public class Product {
private int id;
private String name;
private double price;
private int quantity;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}



}

Output
Iterate list using Enanced for loop
======================================
Id: 100
Name: Product 1
Price: 55.2
Quantity: 20
======================================
Id: 101
Name: Product 2
Price: 54.2
Quantity: 10
======================================
Iterate list using Stream API
======================================
Id: 100
Name: Product 1
Price: 55.2
Quantity: 20
======================================
Id: 101
Name: Product 2
Price: 54.2
Quantity: 10
======================================
Iterate list using for loop
======================================
Id: 100
Name: Product 1
Price: 55.2
Quantity: 20
======================================
Id: 101
Name: Product 2
Price: 54.2
Quantity: 10
======================================
Iterate list using while loop
======================================
Id: 100
Name: Product 1
Price: 55.2
Quantity: 20
======================================
Id: 101
Name: Product 2
Price: 54.2
Quantity: 10
======================================

0 comments:

Post a Comment

ads2