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.
Avoid nested loops where possible (e.g. for loop within another for loop etc) and instead make use of an appropriate java collection.
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.
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.
What are the benefits of the Java Collections Framework? Collections framework provides flexibility, performance, and robustness.