Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Objective:

In the previous article Exception in Java, we have learned how exceptions and exception handling in java works. In this article we will learn about the Collections in Java.

 

Collections:

Collections in java is a framework that provides an architecture to store and manipulate the group of objects. All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections. Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

Collection represents a single unit of objects i.e. a group.

 

What is collections in Framework?

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:

  1. Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.

  2. Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.

  3. Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.

Apart from the Java Collections Framework, the best-known examples of collections frameworks are the C++ Standard Template Library (STL) and Small talk's collection hierarchy. Historically, collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve. We believe that the Java Collections Framework breaks with this tradition, as you will learn for yourself in this chapter.

Benefits of the Java Collections Framework:

The Java Collections Framework provides the following benefits:

Reduces programming effort: By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level "plumbing" required to make it work. By facilitating interoperability among unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.

Increases program speed and quality: This Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. The various implementations of each interface are interchangeable, so programs can be easily tuned by switching collection implementations. Because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to improving programs' quality and performance.

Allows interoperability among unrelated APIs: The collection interfaces are the vernacular by which APIs pass collections back and forth. If my network administration API furnishes a collection of node names and if your GUI toolkit expects a collection of column headings, our APIs will interoperate seamlessly, even though they were written independently.

Reduces effort to learn and to use new APIs: Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a small sub-API devoted to manipulating its collections. There was little consistency among these ad hoc collections sub-APIs, so you had to learn each one from scratch, and it was easy to make mistakes when using them. With the advent of standard collection interfaces, the problem went away.

Reduces effort to design new APIs: This is the flip side of the previous advantage. Designers and implementers don't have to reinvent the wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces.

Fosters software reuse: New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for new algorithms that operate on objects that implement these interfaces.

The root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List. Also see The Collection Interface section.

 

Example 1:

package com.java2novice.iterator;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class MyCollectionIterator {

   public static void main(String a[]){

        

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

       myList.add("Java");

       myList.add("Unix");

       myList.add("Oracle");

       myList.add("C++");

       myList.add("Perl");

       Iterator<String> itr = myList.iterator();

       while(itr.hasNext()){

           System.out.println(itr.next());

       }

   }

}

//Output:

Java

Unix

Oracle

C++

Perl

 

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:

  1. public boolean hasNext() it returns true if iterator has more elements.

  2. public object next() it returns the element and moves the cursor pointer to the next element.

  3. public void remove() it removes the last elements returned by the iterator. It is rarely used.
Example 2:

package com.myjava.listiterator;

import java.util.ArrayList;

import java.util.List;

import java.util.ListIterator;

public class MyListIterator {

   public static void main(String a[]){

       List<Integer> li = new ArrayList<Integer>();

       ListIterator<Integer> litr = null;

       li.add(23);

       li.add(98);

       li.add(29);

       li.add(71);

       li.add(5);

       litr=li.listIterator();

       System.out.println("Elements in forward directiton");

       while(litr.hasNext()){

           System.out.println(litr.next());

       }

       System.out.println("Elements in backward directiton");

       while(litr.hasPrevious()){

           System.out.println(litr.previous());

       }

   }

}

Example Output

Elements in forward directiton
23
98
29
71
5
Elements in backward directiton
5
71
29
98
23


Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner