Two major differences between StringBuffer and StringBuilder ?

  • StringBuffer is synchronized, StringBuilder is not.
  • StringBuffer is safer, StringBuilder is faster.

What is a ConcurrentHashMap ? ConcurrentHashMap vs Collections.synchronizedMap()??

  • A concurrentHashMap is thread-safe implementation of Map interface. In this class put and remove method are synchronized but not get method. This class is different from Hashtable in terms of locking; it means that hashtable use object level lock but this class uses bucket level lock thus having better performance.
  • the differences are below:
    • ConcurrentHashMap vs Collections.synchronizedMap()
    • The main difference between these two is that ConcurrentHashMap will lock only portion of the data which are being updated while other portion of data can be accessed by other threads. However, Collections.synchronizedMap() will lock all the data while updating, other threads can only access the data when the lock is released.
    • Also one other difference is that ConcurrentHashMap will not preserve the order of elements in the Map passed in. It is similar to HashMap when storing data. There is no guarantee that the element order is preserved. While Collections.synchronizedMap(0 will preserve the elements order of the Map passed in. For example, if you pass a TreeMap to ConcurrentHashMap, the elements order in the ConcurrentHashMap may not be the same as the order in the TreeMap, but Collections.synchronizedMap() will preserve the order.
    • Furthermore, ConcurrentHashMap can guarantee that there is no ConcurrentModificationException thrown while one thread is updating the map and another thread is traversing the iterator obtained from the map. However, Collections.synchronizedMap() is not guaranteed on this. If we obtain an Iterator from Collections.synchronizedMap() by calling map.keySet().iterator() and then traverse the iterator, at the same time if another thread is trying to updating the map by calling map.put(K,V), we will get a ConcurrentModificationException.

Why is it preferred to declare: List<String> list = new ArrayList<Stringinstead of ArrayList<String> = new ArrayList<String>();?

  • This is called programming to the interface
  • If later on code needs to be changed from ArrayList to Vector or Linkedlist, then we just need to change the declaration.

What is difference between a HashMap and a HashTable?

  • Hashmap is not synchronized but hashtable is. Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized .
  • A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.
  • Hashmap object values are iterated by using iterator .HashTable uses enumerator to iterate the values of HashTable object.
  • The iterator in Hashmap is fail-fast iterator while the enumerator for Hashtable is not.

If an Employee class is present and its objects are added in an arrayList. Now I want the list to be sorted on the basis of the employeeID of Employee class. What are the steps?

  • Implement Comparable interface for the Employee class and override the compareTo(Object obj) method in which compare the employeeID. Then call Collections.sort() method and pass the list as an argument.
  • Now consider that Employee class is a jar file:
    • Since Comparable interface cannot be implemented, create Comparator and override the compare(Object obj, Object obj1) method .
    • Call Collections.sort() on the list and pass comparator as an argument.

What is difference between an ArrayList and a vector? How can an Arraylist be synchronized without using Vector?

  • The differences are as follows:
    • Synchronization - ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(int i) is surrounded with a synchronized block, thus making Vector class thread-safe.
    • Data growth - Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
    • Performance - Since vector is thread-safe, the performance is slower than ArrayList.
  • Arraylist can be synchronized using:
      Collections.synchronizedList(List list)
      Other collections can be synchronized:
          Collections.synchronizedMap(Map map)
          Collections.synchronizedCollection(Collection c)
    

What is an Iterator ?

  • Java - How to use Iterator?
  • The Iterator interface provides a number of methods that are able to iterate over any Collection. Iterator enables you to cycle through a collection, obtaining or removing elements.
  • ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.
  • Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time.

What differences exist between Iterator and ListIterator ?

  • An Iterator can be used to traverse the Set and List collections, while the ListIterator can be used to iterate only over Lists.
  • The Iterator can traverse a collection only in forward direction, while the ListIterator can traverse a List in both directions.
  • The ListIterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc.

What is the difference between HashSet and TreeSet ?

  • The HashSet is Implemented using a hash table and thus, its elements are not ordered. The add, remove, and contains methods of a HashSet have constant time complexity O(1).
  • TreeSet is implemented using a tree structure. The elements in a TreeSet are sorted, and thus, the add, remove, and contains methods have time complexity of O(logn).

What is difference between fail-fast and fail-safe ?

What differences exist between HashMap and Hashtable ?

  • Both the HashMap and Hashtable classes implement the Map interface.
  • A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.
  • Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hashtable is suitable for multi-threaded environments.
  • HashMap is fail-fast. Hashtable is fail-safe.

What is difference between Array and ArrayList ? When will you use Array over ArrayList ?

  • Below are the differences:
    • Arrays have fixed size, while an ArrayList is dynamic.
    • Arrays can contain primitive or objects, while an ArrayList can contain only objects.
    • An ArrayListprovides more methods and features, such as addAll, removeAll, iterator, etc.
  • For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this approach makes them slower when working on fixed size primitive data types.

What is difference between ArrayList and LinkedList ?

  • Both the ArrayList and LinkedList classes implement the List interface
  • ArrayList provides random access to its elements with a performance equal to O(1). For LinkedList, the search operation for an element has execution time equal to O(n).
  • The Insertion, addition and removal operations of an element are faster in a LinkedList compared to an ArrayList, because there is no need of resizing an array or updating the index when an element is added in some arbitrary position inside the collection.
  • A LinkedList consumes more memory than an ArrayList, because every node in a LinkedList stores two references, one for its previous element and one for its next element.

What is the difference between List, Set and Map?

  • Duplicate:
    • List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes.
    • Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements.
    • Map stored the elements as key & value pair. Map doesn’t allow duplicate keys while it allows duplicate values.
  • Null values:
    • List allows any number of null values.
    • Set allows single null value at most.
    • Map can have single null key at most and any number of null values.
  • Order:
    • List and all of its implementation classes maintains the insertion order.
    • Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order.
    • Map also doesn’t stores the elements in an order, however few of its classes does the same. For e.g. TreeMap sorts the map in the ascending order of keys and LinkedHashMap sorts the elements in the insertion order, the order in which the elements got added to the LinkedHashMap.