Saturday, August 31, 2013

Java - Confusing Terms

Iterable vs Iterator

Implementing Iterable allows an object to be the target of the "foreach" statement. It is in java.lang package. It has one method - Iterator<T> iterator().
Iterator represents iterator over a collection. It is in java.util package. It has three methods - hasNext, Next and remove.

Collection vs Collections

Collection is the root interface in the collection hierarchy. It is in package java.util. This interface is typically used to pass collections around and manipulate them where maximum generality is desired. It implements java.util.Iterable.
Collections class consists exclusively of static methods that operate on or return collections. It doesn't extend/implement any class/interface from collections hierarchy.


Comparator vs Comparable

Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method. Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
Comparable has int compareTo(T o) method. It is in java.lang package.


A comparator object (a class that implements Comparator interface) can compare any two objects and these two objects are passed as parameters to compare() method.

Comparator is used to sort objects in other than its natural ordering.
Comparator has int compare(T o1, T o2) and equals method. It is in java.util package.
 

Following is the result of the compare methods:
    a) A positive integer value if obj1 is greater than obj2.
    b) 0 value if obj1 and obj2 are equal.
    c) A negative integer value if obj1 is less than obj2.



Hashmap vs Hashtable

  • HashMap is non-synchronized whereas Hashtable is synchronized.
  • Java 5 introduced ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.
  • HashMap allows null values as key and value whereas Hashtable doesn’t allow nulls
  • HashMap does not guarantee that the order of the map will remain constant over time.
  • HashMap can be synchronized by:
    • Map m = Collections.synchronizeMap(hashMap);

Iterator vs Enumeration

  • Iterator has a remove() method while Enumeration doesn't.
  • Enumeration is very basic and twice as fast as Iterator and uses very less memory. 
  • Iterator is more secure and safe as compared to Enumeration because it  does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.
  • Enumeration has two methods and both are available in iterator. Method names for both of them are shortened.

 Fail Fast vs Fail Safe

  • Fail fast means not concealing the error, bringing it out as soon as its detected.
  • Business is obstructed, but we get a chance to rectify.
  • Fail-Fast results in interrupted availability, over a period it results in a robust system.
  • Fail ensures system doesn't continue after a failure and creates data corruption and irreversible changes.
  • Fail Fast is a better approach in most scenarios but for life critical, mission critical applications we need fail safe programs and not fail fast. Fail-Safe is Achieved with Exception Handling.

Fail Fast Iterator vs Fail Safe Iterator

  • Difference between Fail Safe Iterator and Fail Fast Iterator is same as discussed above.
  • Iterator of CopyOnWriteArrayList is an example of fail-safe Iterator also iterator written by ConcurrentHashMap keySet is also fail-safe iterator and never throw ConcurrentModificationException in Java.

ConcurrentHashMap vs Collections.synchronizedMap vs Hashtable vs Hashmap

  • Collections.synchronizedMap provide a basic thread-safe implementation of Map. 
  • Collections.synchronizedMap has fail-fast iterator and single collection-wide lock, which at times is obstruction to scalability and concurrency. 
  • Collections.synchronizedMap and Hashtable give poor performance if the size is large due to single collection-wide lock.
  • ConcurrentHashMap implementations provide much higher concurrency while preserving thread safety. 
  • ConcurrentHashMap introduced concept of segmentation , ConcurrentHashMap only locked certain portion of Map to provide thread safety so many other readers can still access map without waiting for iteration to complete.
  • ConcurrentHashMap do not allow null keys or null values while HashMap allows null keys.

Interfaces vs Abstract Classes

  • Interface in Java can only contains declaration.
  • Abstract class may contain both abstract and concrete methods, which makes abstract class an ideal place to provide common or default functionality.
  • A class can inherit multiple interfaces, but it can inherit only one abstract class.
  • By extending abstract class, a class can only participate in one Type hierarchy but by using interface it can be part of multiple type hierarchies.
  • Methods in an abstract class can be protected, private, static etc. But, all items in an interface are implicitly public. 
  • Abstract classes can contain fields that are not static and final.
  • Interface defines a CAN-DO relationship, Classes (including Abstract) than IS-A relationship.
  • In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

Vector vs ArrayList

  • Vector and ArrayList are derived from AbstractList and implements List interface, which means both of them are ordered collection and allows duplicates.
  • Vector vs ArrayList is that both are index based Collection and you can use get(index) method to retrieve objects from Vector and ArrayList.
  • All the methods of Vector are synchronized and methods of ArrayList are not synchronized.
  • Both the ArrayList and Vector hold onto their contents using an Array.
  • A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

Vector vs ArrayList vs LinkedList

  • LinkedList is implemented as a double linked list. Its performance on add and remove is better than Arraylist, but worse on get and set methods.
  • LinkedList also implements Queue interface which adds more methods than ArrayList and Vector, such as offer(), peek(), poll(), etc.
  • While an ArrayList uses an array internally to store the data, a LinkedList uses nodes to hold the elements.
  • LinkList is a series of instances of a private nested class Entry, which has next, previous and element references.

Dictionary & Properties

  • Dictionary is an abstract class. Map is an interface.
  • Dictionary is parent class of Hashtable.
  • Properties is child class of Hashtable.
  • Properties represents a persistent set of properties.
  • Each key and its corresponding value in the property list is a string, while in hashtable it can be any object.
  • Properties is thread safe.
  • Properties is a very specialized class that's designed to hold configuration and/or resources that are usually stored in some file.
  • Properties supports reading and writing its content to a well-defined plain-text format (using load()/store())
  • Properties supports reading and writing its content to a well-defined XML-based format (using loadFromXML()/storeToXML())
  • The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values.

Wait vs Sleep

  • sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock. 
  • wait() allows thread to release the lock and goes to suspended state. The thread is only active when a notify() or notifAll() method is called for the same object.

Notity vs Notify All

  • notify( ) wakes up the first thread that called wait( ) on the same object.
  • notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first. 

Java SE(Standard Edition) vs Java EE(Enterprise Edition) vs Java ME(Micro Edition)

  • Java SE - Plain vanilla version of Java. Java SE has a large library of code which includes many utilities, algorithms, data structures etc. Java EE and Java ME are built on top of Java SE.
  • Java EE - Builds on Java SE. Java EE contains additional libraries of code and development tools that are uniquely useful in developing web-based business applications.
  • Java ME - A reduced version of Java SE and an associated library of software that addresses the unique needs of Java applications that should run on limited capability devices like mobile phones and PDAs.


The list will continue, please contribute.