View RSS Feed

Recent Blogs Posts

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

    by , 02-19-2012 at 07:55 AM
    One of the following methods should be used to iterate the List.

    1. Iterator
    2. get(index)

    get(index) method is quite slower in comparison with Iterator.

    Consider this example:

    Java Code: This is the code to explain Iterator
    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++)
    ...
    Categories
    Linked List
  2. Using a Collection vs. Using an Iterator

    by , 11-28-2011 at 07:36 PM
    In this tip, we will look at determining the appropriate use of a collection versus using an iterator. One of the main reasons for using an interface is that you can create more generic and flexible code. By writing to an interface rather than an implementation, you can reuse your code for more types of objects.

    The root interface for all sequence containers is Collection. The java.utiLAbstractCollection class provides a default implementation for a Collection, so that you can create ...
  3. Enumerations and Iterators

    by , 11-08-2011 at 07:31 PM (My Java Tips)
    An Enumeration is used for iterating over a given collection, Usually of unknown size. Iterator also has the same purpose but Enumeration does not allow modification of the collection, Which can be achieved using Iterator.

    Iteratorís "remove" method removes from the underlying collection the last element returned by the iterator. This method can be called only once per call to the Iterator's "next" method.

    Following code sample will help you understand ...