Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
martin_bschen
Explorer
3,071
In Java, you manage collections of data with .... (surprise) Collections. They are implemented as Classes in the library of the language and have a relative clean interface. In might be tempting to implement such a Collection class in ABAP in order to have a similar interface as the Java language. But this is not the ABAP way to do it. The documentation says:
In Java, all superior data objects (especially container variables such as strings) are modeled using classes. By contrast, ABAP provides very powerful, predefined types. Besides the predefined ABAP strings, internal tables are also provided that are used for structured data storage. These tables represent the most powerful ABAP type. Therefore, it is generally not beneficial to implement own container types using ABAP classes.

In this blog post, I want to compare the usage of a Set in both ABAP and Java. So let's start with the java version:
// Construction of a set
HashSet<Integer> hashset = new HashSet<>();

// Adding '1', success should be true
success = hashset.add(1);

// Adding '2, success should be true
success = hashset.add(2);

// Adding '2', success should be false
success = hashset.add(2);

// Checking existence, exists should be true
exists = hashset.contains(2);

// Checking existence, exists should be false
exists = hashset.contains(3);

// Write number of elements: should be 2
System.out.println(hashset.size());

// Iterate over elements
for (int i: hashset) {
System.out.println(i);
}

// Remove an element
hashset.remove(2);

// Write number of elements: should be 1
System.out.println(hashset.size());

Now, let us compare wit the ABAP version:
" Construction of a set
DATA: hashset TYPE HASHED TABLE OF int4 WITH UNIQUE KEY table_line.

" Adding '1', success should be true
INSERT 1 INTO TABLE hashset.
success = xsdbool( sy-subrc = 0 ).

" Adding '2', success should be true
INSERT 2 INTO TABLE hashset.
success = xsdbool( sy-subrc = 0 ).

" Adding '2', success should be false
INSERT 2 INTO TABLE hashset.
success = xsdbool( sy-subrc = 0 ).

" Checking existence, exists should be true
exists = xsdbool( line_exists( hashset[ table_line = 2 ] ) ) .

" Checking existence, exists should be false
exists = xsdbool( line_exists( hashset[ table_line = 3 ] ) ) .

" Write number of elements: should be 2
WRITE: / 'Number of elements:' , lines( hashset ).

" Iterate over elements
LOOP AT hashset ASSIGNING FIELD-SYMBOL(<integer>).
WRITE: / <integer>.
ENDLOOP.

" Remove an element
Delete table hashset with TABLE KEY primary_key COMPONENTS table_line = 2.

" Write number of elements: should be 1
WRITE: / 'Number of elements:' , lines( hashset ).

This is surprisingly similar. The main difference are that the operations in Java are calls to the methods of the set, while in ABAP you manipulate the set by the keywords of the language.

The Java interface is much cleaner, you can see how you can manipulate a set at one place in the class documentation. The information in ABAP is scattered around the language documentation. So why not implement a set class in ABAP ? (Side question: How would you deal with different types? Java used generics as solution. Feel free to post you ideas in the comments.) In the beginning, I said the is not "the ABAP way". Of course this is not a real reason. The first one I can come up is performance. Second, if you understand the manipulation described above, there is no real reasons to encapsulate it behind a class. ( Do you see further reasons? Again, feel free to post in the comments!).

One last remark: If you find yourself writing code like
Sort itab.
Delete adjacent duplicates from itab.

chances are, that you really want a set and should use it as described above. The two lines are not needed anymore then.
15 Comments
Labels in this area