What is the difference between “==” and equals(…) method? What is the difference between shallow comparison and deep comparison of objects?

When you are using a collection of objects e.g. using a java.util.Set of persistable Hibernate objects etc. It is easy to implement these methods incorrectly and consequently your program can behave strangely and also is hard to debug. So, you can expect these questions in your interviews.

== [ shallow comparison ] equals( ) [deep comparison ] The == returns true, if the variable reference points to the same object in memory. This is a “shallow comparison”. The equals() – returns the results of running the equals() method of a user supplied class, which compares the attribute values. The equals() … Click here to continue reading.

What are some of the best practices relating to Java collection?

  • Use ArrayList, HashMap etc as opposed to Vector, Hashtable etc, where possible to avoid any synchronization overhead. Even better is to use just arrays where possible. If multiple threads concurrently access a collection and at least one of the threads either adds or deletes an entry into the collection, then the collection must be externally synchronized. This is achieved by:
  • Set the initial capacity of a collection appropriately (e.g. ArrayList, HashMap etc). This is because Collection classes like ArrayList, HashMap etc must grow periodically to accommodate new elements. But if you have a very large array, … Click here to continue reading.

What are static factory methods?

Some of the above mentioned features like searching, sorting, shuffling, immutability etc are achieved with java.util.Collections class and java.util.Arrays utility classes. The great majority of these implementations are provided via static factory methods in a single, non-instantiable (i.e. private constrctor) class. Speaking of static factory methods, they are an alternative to creating objects through constructors. Unlike constructors, static factory methods are not required to create a new object (i.e. a duplicate object) each time they are invoked (e.g. immutable instances can be cached) and also they have a more meaningful names like valueOf, instanceOf, asList etc. For example:
Instead … Click here to continue reading.

What is a list iterator?

The java.util.ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction (i.e. forward and or backward) and modify the list during iteration.JAVA collection framework
What are the benefits of the Java Collections Framework? Collections framework provides flexibility, performance, and robustness.

  • Polymorphic algorithms – sorting, shuffling, reversing, binary search etc.
  • Set algebra – such as finding subsets, intersections, and unions between objects.
  • Performance – collections have much better performance compared to the older Vector and Hashtable classes with the elimination of synchronization overheads.
  • Click here to continue reading.