Thorsten Niehues
Thorsten Niehues

Reputation: 14472

How to update a ABAP Hashed table

How to update an ABAP Hased table?

What is the equivalent of the Java statement:

hashMap.put("myKey", "myValue");

in ABAP?

Upvotes: 2

Views: 5481

Answers (2)

Gert Beukema
Gert Beukema

Reputation: 2565

Assuming that you are only interested in the effect put() has on the content of hashMap and don't care about the value it returns, the equivalent would be:

INSERT VALUE #( KEY = 'myKey' VALUE = 'myValue' ) INTO TABLE hashMap.

With the difference that for an existing key the entry will not be updated but SY-SUBRC will be set to 4, so you'd have to do some extra work. Internal table hashMap needs to be defined as a HASHED TABLE WITH UNIQUE KEY KEY and a type that has at least the fields KEY and VALUE.

Also see: SAP Help

Upvotes: 6

Michał Majer
Michał Majer

Reputation: 263

Working example:

TYPES: BEGIN OF LINE,
    COL1,
    COL2,
  END OF LINE.

DATA:   WA TYPE LINE,
      ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

WA-COL1 = 'X'. INSERT WA INTO TABLE ITAB.
WA-COL1 = 'Y'. INSERT WA INTO TABLE ITAB.
WA-COL1 = 'Y'. INSERT WA INTO TABLE ITAB. "Not added

Upvotes: 5

Related Questions