dcsimg
 

Submitted by: M S Sridhar(sridhar.ms@gmail.com)

Tuesday Jul 31st 2018 by M S Sridhar

Language: Java, Expertise: Intermediate - See how easy it is to iterate a list and print the values with double colon in Java.

Iterating a list and printing the values is very simple, as illustrated by the code snippet below.

import java.util.*;

public class DoubleColon
{
   public static void main(String args[])
   {
      DoubleColon doubleColon = new DoubleColon();
      doubleColon.proceed();
   }
   
   private void proceed()
   {
      List oddNumbers = new ArrayList();
      //List oddNumbers is created and populated with some odd numbers
      oddNumbers.add(1);
      oddNumbers.add(3);
      oddNumbers.add(5);
      oddNumbers.add(7);
      oddNumbers.add(9);

      System.out.println("Contents of the oddNumbers list");
      //Using the :: here. This explicitly understands and prints the items in the list
      oddNumbers.forEach(System.out::println);   
   }
}

/*

Expected output:

[root@mypc]# java DoubleColon
Contents of the oddNumbers list
1
3
5
7
9

*/
Home
Mobile Site | Full Site