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. What are the benefits of the Java Collections Framework? Collections framework provides flexibility, performance, and robustness. Polymorphic algorithms – sorting, shuffling,… Continue reading

Why do you get a ConcurrentModificationException when using an iterator?

Problem : The java.util Collection classes are fail-fast, which means that if one thread changes a collection while another thread is traversing it through with an iterator the iterator.hasNext() or iterator.next() call will throw ConcurrentModificationException. Even the synchronized collection wrapper classes SynchronizedMap and SynchronizedList are only conditionally thread-safe, which means… Continue reading

How to implement collection ordering?

SortedSet and SortedMap interfaces maintain sorted order. The classes, which implement the Comparable interface, impose natural order. By implementing Comparable, sorting an array of objects or a collection (List etc) is as simple as: Arrays.sort(myArray); Collections.sort(myCollection); // do not confuse “Collections” utility class with the // “Collection” interface without an… Continue reading

Explain the Java Collections Framework?

The key interfaces used by the collections framework are List, Set and Map. The List and Set extends the Collection interface. Should not confuse the Collection interface with the Collections class which is a utility class. Set (HashSet , TreeSet) List (ArrayList, LinkedList, Vector etc) A Set is a collection… Continue reading

What is the difference between a stack and a queue?

Queue Stack First item to be inserted is the first one to be removed. Allows access to only last item inserted. This mechanism is called First In First Out (FIFO). An item is inserted or removed from one end called the “top” of the stack. This is called Last In… Continue reading

What is the main difference between an ArrayList and a Vector? What is the main difference between HashMap and Hashtable?

Vector / Hashtable ArrayList / HashMap Original classes before the introduction of Collections API. Vector & Hashtable are synchronized. Any method that touches their contents is thread-safe. So if you don’t need a thread safe collection, use the ArrayList or HashMap. Why pay the price of synchronization unnecessarily at the… Continue reading

When is a method said to be overloaded and when is a method said to be overridden?

Method Overloading Method Overriding Overloading deals with multiple methods in the same class with the same name but different method signatures. class MyClass { public void getInvestAmount(int rate) {…} public void getInvestAmount(int rate, long principal) { … } } Both the above methods have the same method names but different… Continue reading

Why there are some interfaces with no defined methods (i.e. marker interfaces) in Java?

The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example java.io.Serializable, java.lang.Cloneable, java.util.EventListener etc. Marker interfaces are also known as “tag” interfaces since they tag all the… Continue reading

When to use an interface?

For polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently, you should prefer using interface to abstract. Coding to an interface reduces coupling and interface inheritance can achieve… Continue reading