Collections Framework Java


    A data structure (DS) can be termed as a storage box of data, or to say, a collection of elements. To the storage, further elements can be added, existing elements can be retrieved, deleted or replaced or can be copied to another DS. In Java, a data structure is a class (just like any other Java class) grouping multiple elements as a single entity (unit). Contrary to arrays, a data structure grows dynamically when more elements are placed. Coming to java, some data structures allow duplicate elements and some do not; some will accept null values and some will print the elements automatically in ascending order.

    Collections or Collections Framework Java

    All the DS that comes with JDK1.2 are put together called as collections framework as the basic interface from which all the DS are derived is Collection. The collections framework comes with many interfaces like Collection, List, Set, Map and SortedMap etc. From these interfaces many DS are derived like LinkedList, TreeSet, ArrayList and HashMap etc.

    All the collections framework classes are placed in java.util package. The java.util package comes with many other service classes like Date, GregorianCalender, StringTokenizer and Random etc.

    What is a framework?

    A framework can be termed as a fundamental entity or structure developed to derive solutions for many complicated problems. A framework includes interfaces, classes, support programs, many resources and all converted into an API. A framework is a reusable software theoretically designed to work as a guide. Many frameworks exist in the market like Spring and Struts etc. A framework is architecture with interoperability between objects guided by specific design rules.
    The designers started their collections framework with the introduction of basic interfaces like Map, Collection, List and Set. Being interfaces, they give common functional methods for many of their derived classes. The root interface of all is Collection. A point to note here is, collections also includes Map and SortedMap interfaces that are not derived from Collection. Map and SortedMap have their own separate hierarchy; but still placed in Collections frame work. Complete hierarchy is available at Java Collections Interfaces Hierarchy.

    I. Collections Framework Java Grouping

    All the interfaces and their derived classes can be grouped into three core groups.

    1. Interfaces: Three interfaces Collection, List and Map form the super classes for many concrete classes. These interfaces give methods which subclasses implement so that all the subclasses have common methods. It is easy to the programmer to remember. For example, to add an element for any framework class is add() defined in Collection class.
    2. Implementations: All the classes that implement the above three interfaces are known as Collections Framework (infact, these are data structures) and include LinkedList, TreeSet etc.
    3. Algorithms: Algorithms make programmer life very easy. They are a bunch of static methods with which DS elements can be manipulated. For example, obtaining maximum and minimum values or sorting or filling all the elements with some values can be done in no time. Java algorithms can be compared with STL of C++.
    II. Advantages

    Following gives important advantages or features of collections framework classes.

    1. Reduces programming effort with Software reusability: Designers took utmost care to avoid unnecessary coding by the programmer for small simple operations like sorting, shuffling, searching and knowing the frequency of an element occurring in a DS. This avoids redundancy and increases productivity. These methods can be used as it is by the the developer without any modifications.
    2. Speed of execution and durability: Execution speed depends on the DS you choose depending on your coding needs. Some DS methods are synchronized and offers data consistency in multi threaded environments; but being synchronized reduces performance. Choosing of DS should consider the requirements like number of times of DS modification by adding new elements or retrieving is more than the addition, data consistency required for critical important data etc. Here comes node-based and array-based DS (in node-based, addition will be faster and in array-based, retrieval is faster). Programmer should be clever enough to choose a correct DS for his needs taking into these number of factors into consideration.
    3. Allows interoperability: One DS elements can be passed to another DS within no time. Or one DS can be assigned to another DS, a DS can be cloned or a synchronized version of the existing DS can be obtained without disturbing the original DS. This is possible because the super class of all DS is Collection interface. It is one of the great design aspects designers followed. The node-based DS elements can be converted into array-based DS.
    4. Reduces effort to practice new classes: As super class of all collections is Collection interface, all the subclasses come with common methods with which a DS can be manipulated. Collections is a very matured framework and programmer need not depend on other languages for any additional functionality.
    III. Disadvantages
    1. Care in casting: Casting in between the framework classes should be done with maximum care like generics data types compatibility.
    2. Runtime checks: Many DS throw runtime exceptions and programmer will be happy if compile-time checks also exist.

    Also know about the basic collections interfaces from which all data structures are derived.

4 thoughts on “Collections Framework Java”

  1. interview question

    where we use Collection classes and interface concept in web based project
    especially retrieving data from date base

Leave a Comment

Your email address will not be published.