Skip to main content

Posts

Showing posts with the label Iterator

Difference between Enumeration and Iterator

Use Iterator always for better programming. One big difference between Iterator and Enumeration is (apart from what Pallav has already mentioned), iterator is fail-safe. If you are using an iterator to go through a collection you can be sure of no concurrent modifications in the underlying collection which may happen in multi-threaded environments. Well, apart from being an advantage this also is said to decrease performance by a tad. However, that's negligible. (Some amount of processing is involved in checking for modifications). If you see the docs it says about "well defined semantics" for the iterator, the fail-safe property adds to that. Enumeration and Iterator are interfaces in java.util package. Enumeration is the old Interface(for legacy classes like Hashtable). Iterator is for the new classes like HashSet,HashMap.. Enumeration and Iterator are used to just count the no of elements and view it. But Iterator has an additional method for deleting elements...

Collection Interview Questions

Q1) What is difference between ArrayList and Vector? Ans: ) 1) 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 and thus making Vector class thread-safe. 2) 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. Q2) How can Arraylist be synchronized without using Vector? Ans) Arraylist can be synchronized using: Collection.synchronizedList(List list) Other collections can be synchronized: Collection.synchronizedMap(Map map) Collection.synchronizedCollection(Collection c)

Why doesn't Iterator work for my collection?

Let's take a look at the following code: class Program { public static void main(String args[]) { ArrayList alist = new ArrayList (); alist.add(new String("A")); alist.add(new String("B")); alist.add(new String("C")); int i = 0; for (Iterator it = alist.iterator(); it.hasNext(); ) { System.out.println(alist.get(i++)); } } } A runtime exception java.lang.IndexOutOfBoundsException is thrown when it goes beyond the end. What is wrong? The code combines the iterator and index. After hasNext() returns true, the only way to advance the iterator is to call next() . But the element is retrieved with get(index) , so the iterator is never advanced. In the above example, the hasNext() will always be true, and eventually the index i for get(index) will beyond the end of ArrayList .

What is the advantage of using an Iterator compared to the get(index) method?

You can navigate or access a List by using the get(index) method or an Iterator. Sometimes the get(index) method is your only option, and sometimes it's slightly faster than an Iterator. Other times, however, it can be much, much slower than an Iterator. For example, a LinkedList is a classic example. This class has a get(index) method but it is very slow. Well, it's not that bad if the list is short, or if you're looking for an item that is close to the beginning or end. But if you need to access the List frequently, you will see a big difference. Let's take a look at the following example: public class TestClass { public static void main(String[] args) { int len = 100000; LinkedList linkedLst = new LinkedList(); ArrayList arrayLst = new ArrayList(); for (int m =0; m!= len; m++) { int x = (int)Math.random(); linkedLst.add(x); arrayLst.add(x); }