Pin It

How to hold your objects using Collection Interface in Java ?

Program to demonstrate how to hold your objects using Collection Interface in Java

package com.hubberspot.example;


// 1. We have to import two Collections
// framework class and interface as 
// ArrayList and Collection

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {

 public static void main(String[] args) {

  // 2. Creating a Collection reference variable
  // for holding the objects. Collection is interface
  // which is the base interface for all the collections
  // such as ArrayList, Queue etc. Here we have created 
  // an ArrayList object which is holding the String objects
  // and its object is been referenced by Collection 

  Collection<String> collections = new ArrayList<String>(); 

  // 3. Adding five String objects to Collection such as 
  // "www", ".", "hubberspot", ".", "com"

  collections.add("www");  
  collections.add(".");
  collections.add("hubberspot");
  collections.add(".");
  collections.add("com");
  System.out.println("The Collection objects are : ");

  // 4. Looping each Collection object and printing it on 
  // console

  for(String s : collections) {
   System.out.print(s);
  }

 }

}

Output of the program :




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot