James Parker James Parker - 1 month ago 4
Java Question

How do I handle an empty java set of strings in AWS DynamoDB

I'm trying to store an array of strings within an AWS DynamoDB table. For the most part this array will be populated with at least one string. However there is the case where the array could be empty.

I've created a DynamoDB model in a Java Lambda function that has a set of strings as one of it's properties. If I try to save a DynamoDB model when the set of strings is empty it gives me an error saying I can't store an empty set in DynamoDB.

So my question is, how would handle removing that set property when it's empty from my model before I save / update it in the DynamoDB?

Here's an example of the model.

@DynamoDBTable(tableName = "group")
public class Group {
private String _id;
private Set<String> users;

public String getId() {
return _id;

public void setId(final String id) {
_id = id;

public Set<String> getUsers(){
return users;

public void setUsers(final Set<String> users) {
this.users = users;

public void addUser(String userId) {
if(this.users == null){
this.setUsers(new HashSet<String>(Arrays.asList(userId)));

First time when I will create a group. It could have no user, or it could have one or more user.


This is somewhat of an old question but the way I would solve this problem is with a custom DynamoDBMarshaller.

Making use of the @DynamoDBMarshalling annotation, you can decorate the POJO accessor methods in order to dictate to the DynamoDB mapper which marshaller class to use to serialize and deserialize the set of strings. This way you get control over the special use cases.

Here is also a link to an AWS blog post with an example

The one caveat with the approach above is the customer marshaller solution serializes and deserializes to/from string so the representation in the database wouldn't be a set per se. However, I wouldn't consider that to be too bad.

Another approach might be to use the Document API, instead of the object mappers, which gives you full control over the items. Though I would still go for custom mapper with string backing.