Sunday, August 22, 2010

Java Collection Framework - Overview

Core Collection Interfaces
The core interfaces that define common functionality and allow collections to be manipulated independent of their implementation.

The 6 core Interfaces used in the Collection framework are:
    1. Collection
    2. Set
    3. List
    4. Iterator (Not a part of the Collections Framework)
    5. SortedSet
    6. Map
    7. SortedMap
    Note: Collection and Map are the two top-level interfaces.

    Collection Interface
    Concrete Classes
    The concrete classes that are specific implementations of the core interfaces, providing data structures that a Java program can use.



    Collection Overview
    Java ArrayList

        * Resizable-array implementation of the List interface. A java ArrayList is used to store an "ordered" group of elements where duplicates are allowed.
        * Implements all optional list operations, and permits all elements, including null.
        * This class is similar to Vector, except that it is unsynchronized.        
        * The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. ArrayList's give great performance on get() and set() methods, but do not perform well on 
           add() and remove() methods when compared to a LinkedList.
        * An ArrayList capacity is the size of the array used to store the elements in the list. As elements are added to an ArrayList, its capacity grows automatically. It is an Array based 
           implementation where elements of the List can be accessed directly through get() method.

    To prevent unsynchronized access to the list: List list = Collections.synchronizedList(new ArrayList(...));

    Java Linked List
        * A LinkedList is used to store an "ordered" group of elements where duplicates are allowed.
        * A LinkedList is based on a double linked list where elements of the List are typically accessed through add() and remove() methods.
    Java Vector
    public class Vector extends AbstractList implements List, RandomAccess, Cloneable, Serializable 
        * The Vector class implements a growable array of objects where the size of the vector can grow or shrink as needed dynamically.
        * Like an array, it contains components that can be accessed using an integer index.
        * An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.
    Map Interface




    Map Overview


    Saturday, August 21, 2010

    OOPS Concepts - Inheritance

    Inheritance
    It is the inclusion of behavior (i.e. methods) and state (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. The key benefit of Inheritance is that it provides the formal mechanism for code reuse. Any shared piece of business logic can be moved from the derived class into the base class as part of refactoring process to improve maintainability of your code by avoiding code duplication. The existing class is called the superclass and the derived class is called the subclass. Inheritance can also be defined as the process whereby one object acquires characteristics from one or more other objects the same way children acquire characteristics from their parents.

    There are two types of inheritances:
    1. Implementation inheritance (aka class inheritance):
      You can extend an application’s functionality by reusing functionality in the parent class by inheriting all or some of the operations already implemented. In Java, you can only inherit from one superclass. Implementation inheritance promotes reusability but improper use of class inheritance can bcause programming nightmares by breaking encapsulation and making future changes a problem. With implementation inheritance, the subclass becomes tightly coupled with the superclass. This will make the design fragile because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking them. So when using implementation inheritance, make sure that the subclasses depend only on the behavior of the superclass, not on the actual implementation. For example in the above diagram, the subclasses should only be concerned about the behavior known as area() but not how it is implemented.
    2. Interface inheritance (aka type inheritance):This is also known as subtyping. Interfaces provide a mechanism for specifying a relationship between otherwise unrelated classes, typically by specifying a set of common methods each implementing class must contain. Interface inheritance promotes the design concept of program to interfaces not to implementations. This also reduces the coupling or implementation dependencies between systems. In Java, you can implement any number of interfaces. This is more flexible than implementation inheritance because it won’t lock you into specific implementations which make subclasses difficult to maintain. So care should be taken not to break the implementing classes by modifying the interfaces.
    Which one to use? 
    Prefer interface inheritance to implementation inheritance because it promotes the design concept of coding to an interface and reduces coupling. Interface inheritance can achieve code reuse with the help of object composition. If you look at Gang of Four (GoF) design patterns, you can see that it favors interface inheritance to implementation inheritance.

    Interview Questions

    1. Using == and equals()When you really need to know if two references are identical, use ==. But when you need to know if the objects themselves (not the references) are equal, use the equals().

    Coding Tips - Converting Char to Int

    Suppose if we have mobile number in the string value and need to find its sum of numbers

    int sum=0;
    for(int j=0;j<(tmp[i]).length();j++){
        int a=(tmp[i].charAt(j))-'0'; // here is the trick
        sum=sum+a;
        System.out.println("No = "+a+" & Sum ="+sum);
    }
    Another way ---
    int sum = 0;
    int length = mobNo.length();
    for (int i = 0; i < length; i++){
        sum = sum + Integer.parseInt(String.valueOf(mobNo.charAt(i))); // here is the trick
    }