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.
// 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());
" 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 ).
Sort itab.
Delete adjacent duplicates from itab.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 |