Skip to Content
author's profile photo Former Member
Former Member

Abap Iterating and Iterator Classes

In the comments of the conversation Top 10 ABAP crimes a few of us were discussing the use of an iterators and iterator classes. Below I will add what has already been presented:

Fabio Pagoti

I have read about it but I had never used. What I usually do is just encapsulating an internal table inside a class and not modelling table rows as objects (I hope this was clear). In this case I'm not sure if the iterator class can be used and useful.

Matthew Billingham

I have used iterators for particular complex data structures. Underneath was an internal table of references, and I used indexes to keep track. I could of course have looped through the internal table at the higher level instead of using an iterator, but due to the data complexity, I really didn't want to expose the internal table at that level.

Joao Sousa

I do in complex application (in my latest one I don't use loops except to deal with BAPIRET), because it's the way you code in C# and Java, and that way I can easily translate the code to C#. Basically for large modules I program as if I was coding in C#.

If you use Collections and HashMaps it's the only way to "loop", although I do miss the typed List<T> and HashMap<T,X>.

PS: Actually it's not the only way, there's the get_values_table or something 😊.

I use CL_OBJECT_MAP and CL_OBJECT_COLLECTION which are the equivalent to HashMap and List the problem is that these are not typed lists and so I have to rely on casts that the compiler doesn't check for correctness.

I usually write the type in the description of the return parameter of the method.

Jeffrey Vander Graaf

  1. I used Iterator classes heavily in java and C#, not in abap yet. I want to use them again. I relied heavily on array list, hash maps, lists, etc when programming in other languages.
  2. Restrictions...hmm, in java i can't critique, they are awesome. In abap, the primary hurdle would probably be generics. Abap does generics differently than java but I assume adding a constructor to initially set the type of object of the list would solve that. Adding a check to make sure objects of that type can only be added would be next. Iterator classes can be implemented from anywhere an object can be implement. If I run into any other hurdles i'll let you know.
  3. Too many, If I ever needed a place to hold multiple objects of the same type I'd use an iterator class. They provide a wide range of functionality for accessing a list of objects, and follows the object oriented design model. From what I was taught at University and experienced in the field, data structures such as link lists are essential.

What is everyone's opinion on iterator classes and the comments above?

Here are some useful links to similar postings:

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Mar 05, 2015 at 01:40 AM

    Hi Jeffrey,

    I added a comment on that blog post, but will duplicate it here. This is a nice blog post on this subject: Become a JAVAPER - build your own ArrayList and Iterator



    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hey Custodio,

      That's a solid article. I just glanced at it quickly and from the looks of it, will be quite helpful.

      Love the term JAVAPER, was never a fan of the ABAPER name, but i can do JAVAPER.

      I'm currently implementing the java.util.logger package in abap so their interpretation of java code and data structure implementation should be useful.

      The info is much appreciated bro,


  • Posted on Mar 03, 2015 at 11:58 PM

    Hello Jeffrey,

    I routinely implement iterators for internal tables using local classes like

    CLASS lcl_iterator DEFINITION FRIENDS lif_unit_test.


    METHODS constructor IMPORTING it_messages TYPE tt_trace

    iv_start TYPE sytabix DEFAULT 1

    iv_stop TYPE sytabix OPTIONAL.

    METHODS next RETURNING VALUE(rs_data) TYPE ts_message

    RAISING cx_sy_itab_error.

    METHODS has_next RETURNING VALUE(rv_flag) TYPE xsdboolean.



    DATA mv_idx TYPE sytabix.

    DATA mv_size TYPE sytabix.

    DATA mt_list TYPE tt_trace.


    A use case is the event handler of ALV Grid: I pass an iterator and the handler processes selected entries without having to know about the selection or additional validation logic. It is just a case of separation of concern.

    CLASS lcl_iterator IMPLEMENTATION.

    METHOD constructor.

    CLEAR mv_idx.

    mt_list = it_messages. " shared, never changed

    mv_size = COND #( WHEN iv_stop IS INITIAL THEN lines( mt_list )

    ELSE iv_stop ).

    skip( iv_start - 1 ).


    METHOD next.

    skip( ).

    rs_data = mt_list[ mv_idx ].


    METHOD has_next.

    rv_flag = boolc( mv_idx < mv_size ).


    METHOD skip.

    ADD iv_count TO mv_idx.



    Reminder: a new object must be created for each iteration. Access to the table index MV_IDX should be avoided. But if worst comes to the worst...

    this ABAP criminal will avoid ABAP police infested blogs 😊



    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Jacques Nomssi Nzali

      Took a quick glance at blog and i'm excited. Right now I'm on 7.31 so i didn't know that 7.4 existed. I've only ventured into the SCN world over the last weekend and it's already helping quite a bit.

      My apologises for questioning your code, it looked like a java hybrid with the inline conditional and table indexing.

      I like your implementation of the iterator class, I find iterators extremely useful.

      Do you happen to use the iterator with more advanced data structures such as hashmaps, arraylists, lists, etc?

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.