How to display a vehicleList using forEach in java8 programming?

              Java8 offers variety of concepts like lambda expressions, functional interfaces, streams, default methods, collectors and so on…

“Foreach” is an added feature for looping to iterate the elements. It uses lambda expression or looping.

Java Program to display the vehicle list using ForEach method using Lambda expressions:

Steps to follow:

  • ·       This program creates a vehicleList as string.
  • ·       Add the list of vehicles to vehicleList.
  • ·       Using lambda expression, ForEach method dispalys the vehicleList.

//Java Program to display the vehicle list using ForEach method using Lambda expressions:

import java.util.ArrayList; 

import java.util.List; 

public class ForEachEg { 

  public static void main(String[] args) { 

                      List<String> vehicleList = new ArrayList<String>(); 

                      vehicleList.add("Car"); 

                      vehicleList.add("Bike"); 

                      vehicleList.add("Truck"); 

                      vehicleList.add("Bus"); 

                             vehicleList.add("Auto");

                      System.out.println("------------Example of passing lambda expression--------------"); 

                      vehicleList.forEach(vehicle -> System.out.println(vehicle)); 

                  } 

              }

Compile and run the program to get the output as shown below. 

C:\raji\blog>javac ForEachEg.java

C:\raji\blog>java ForEachEg

------------Example of passing lambda expression--------------

Car

Bike

Truck

Bus

Auto

Next program is using foreach as looping.

Java Program to display the vehicle list using ForEach method using Looping:

              This program uses the looping to iterate the elements. It has steps to follow to create this program.

Steps:

  • ·       Import the built in packages java.util.ArrayList and java.util.List
  • ·       Create a public class ForEachEg1 and save the program as “ForEachEg1.java”.
  • ·       Inside the main function, create list as “vehicleList”.
  • ·       Add the vehicles to vehicleList.
  • ·       Print the list using forEach() method.

// Java Program to display the vehicle list using ForEach method using Looping

import java.util.ArrayList; 

import java.util.List; 

public class ForEachEg1 { 

    public static void main(String[] args) { 

        List<String> vehicleList = new ArrayList<String>(); 

              vehicleList.add("Car"); 

              vehicleList.add("Bike"); 

              vehicleList.add("Truck"); 

              vehicleList.add("Bus"); 

              vehicleList.add("Auto");

        System.out.println("------------Iterating by passing method reference---------------"); 

        vehicleList.forEach(System.out::println); 

    } 

} 

  • Open the command prompt. Set the path for java.
  • Compile the program .

C:\raji\blog>javac ForEachEg1.java

  • Run the program to display the output.

C:\raji\blog>java ForEachEg1

------------Iterating by passing method reference---------------

Car

Bike

Truck

Bus

Auto

These are the ways to implement ForEach method to iterate elements.

No comments:

Post a Comment