dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Tip: Understanding the Double Colon in Java

See how easy it is to iterate a list and print the values with double colon in Java.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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

*/

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date