Home Java Javatutorial Collections in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Collections in Java

Advertisement
Collections in Java are data-structures primarily defined through a set of classes and interface and used by Java professionals. Some collections in Java that are defined in Java collection framework are: Vectors, ArrayList, HashMap, LinkedList, Stack and Hashtable. Collection Interface is used to circulate through these collections of objects.

Collections in Java are data-structures primarily defined through a set of classes and interface and used by Java professionals. Some collections in Java that are defined in Java collection framework are: Vectors, ArrayList, HashMap, LinkedList, Stack and Hashtable.

Collections is a group of objects. Objects can be stored, recalled, and controlled as elements of collections. Collection Interface is used to circulate through these collections of objects. Interface uses add(), remove(), toArray() and contains() methods to add, remove, convert collection in to array of elements and to check if a specified element is in the collection respectively.

Collection Interface is a sub-interface of Iterator Interface. The Iterable interface provides the iterator() method used by for-each statements All collections have an iterator that goes through all of the elements in the collection.

Java Collection framework can be used to store phone numbers, to maintain employee record,

Primary types of collections are:

  • Sets
  • Lists
  • Queues
  • Maps*

*Maps are not exactly part of Collection framework but because of their capability to store and manipulate data as collection of objects they are a part of collections.

Set and List extends Collection Interface. While Set does not allow duplicates, Lists allows it. Map extends neither Set nor Collection.

Factors to consider while selecting a collection for a particular problem are:

  • Ordering - Selection of collection can be based on Order of elements.
  • Duplicates - Selection of collection can be based on the duplicability of elements in a collection. That depends on users to whether they want duplicate elements or not.
  • Thread Safety
  • Key-Value pair
  • Random Access
  • Upper Bounds

Following is the example of HashSet collection that displays the methods to add, remove and iterate the values of collection:

import java.util.*;

public class CollectionTest {
  public static void main(String [] args) { 
  System.out.println( "Collection Example!\n" ); 
  int size;
  // Create a collection  
  HashSet collection = new HashSet ();
  String str1 = "Yellow", str2 = "White", str3 = "Green", str4 = "Blue";  
  Iterator iterator;
  //Adding data in the collection
  collection.add(str1);  
  collection.add(str2); 
  collection.add(str3); 
  collection.add(str4);
  System.out.print("Collection data: ");  
  //Create a iterator
  iterator = collection.iterator(); 
  while (iterator.hasNext()){
  System.out.print(iterator.next() + " ");  
  }
  System.out.println();
  // Get size of a collection
  size = collection.size();
  if (collection.isEmpty()){
  System.out.println("Collection is empty");
  }
  else{
  System.out.println( "Collection size: " + size);
  }
  System.out.println();
  // Remove specific data  
  collection.remove(str2);
  System.out.println("After removing [" + str2 + "]\n");
  System.out.print("Now collection data: ");
  iterator = collection.iterator(); 
  while (iterator.hasNext()){
  System.out.print(iterator.next() + " ");  
  }
  System.out.println();
  size = collection.size();
  System.out.println("Collection size: " + size + "\n");
  //Collection empty
  collection.clear();
  size = collection.size();
  if (collection.isEmpty()){
  System.out.println("Collection is empty");
  }
  else{
  System.out.println( "Collection size: " + size);
  }
  }
}
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 19, 2013

Related Tutorials

Ask Questions?    Discuss: Collections in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com