Thursday 21 February 2013

Difference between Enumerator and Iterator in AX 2012


Difference between Enumerator and Iterator(use for list, Maps or Sets element’s traversing)

·         We can traverse our List, Map or Set collections by using either an enumerator or an iterator .
·         We can’t add/Delete elements in the List or Maps or Set using Enumerator class but we can do it via iterator class
·         It is a best practice to use the Enumerator class instead of the Iterator class, because enumerators are automatically created on the same tier as the map (when calling the List.getEnumerator method)
·         Enumerators require less code than iterators to initiate there instance, they perform slightly better.

Why Enumerator introduced?

When collection classes were first introduced in Dynamics AX, the iterator was the only option. But because of a few unwarranted drawbacks that appear as hard-to-find errors, enumerators were added, and iterators were kept for the backward compatibility. Just see the below listed code snippet

List list   = new List(Types::Integer);
ListIterator  iterator;
ListEnumerator  enumerator;
;
//Populate List
…..
…..

//Traverse using an iterator.
iterator = new ListIterator(list);

while(Iterator.more())
{
print iterator.value());
iterator.next();
}

//Traverse using an enumerator
enumerator = list.getEnumerator();

while(enumerator.moveNext())
{
print enumerator.current();
}

The 1st difference is the way iterator and enumerator instances are created. For the iterator, you call new, and for the enumerator, you get an instance from the collection class by calling the getEnumerator method.

In most cases, both approaches will work equally well. However, when the collection class resides on the opposite tier from the tier on which it is traversed, the situation is quite different.For example, if the collection resides on the client tier and is traversed on the server tier, the iterator approach fails because the iterator does not support cross-tier referencing. The enumerator does not support cross-referencing either, but it doesn’t have to because it is instantiated on the same tier as the collection class. Traversing on the server tier using the client tier enumerator is quite network intensive, but the result is logically correct because some code is marked as “Called From”, meaning that it can run on either tier, depending on where it is called from. You could have broken logic if you use iterators, even if you test one execution path. In many cases, hard-to-track bugs such as this surface only when an operation is executed in batch mode.

The 2nd difference is the way traversing happens which is another potential threat as the onus lies on the developer to ensure that he moves the pointer by using .next() method else the code can land into endless loop.So again enumerator is clear winner here

But there is still one scenario while iterator holds edge over enumerator, if you want to delete/insert items from list.See the code snippet below:

List   list = new List(Types::Integer);
ListIterator iterator;
;

list.addEnd(100);
list.addEnd(200);
list.addEnd(300);

iterator = new  ListIterator(list);
while(iterator.more())
{
if(iterator.value() == 200)
iterator.delete();
iterator.next();
}
print list.toString();   //{100,300}
pause;
}


5 comments:

  1. very informative & different one....
    I would like to share the information

    Digital Marketing Company in Chennai

    ReplyDelete
  2. It’s the best time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I want to suggest you few interesting things or suggestions.You can write next articles referring to this article. I desire to read even more things about it..
    SAP Training in Chennai
    SAP Basis Training in Chennai
    SAP SD Training in Chennai
    SAP FICO Training in Chennai

    ReplyDelete
  3. this is very nice post thanks for updating your information to us.it is such a wonderful information about the software development and java is one of growing technology this is useful for me thank you so much.

    Hadoop Training in Chennai

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. how will you check for empty list using enumerator, say if list is empty(filled by some external processes) how will you be able to find using enumerator that your enumerator is empty?

    ReplyDelete