Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Working with data stored in formats like CSV (comma-separated values) or custom-delimited data often necessitates splitting a string into key-value pairs in Java. In this tutorial, we’ll explore how to split Java text into key-value pairs with the help of code examples and explanations.

2. Using StringTokenizer

The StringTokenizer class, which enables us to break a string into tokens depending on a provided delimiter, is one approach to splitting a string into key-value pairs.

Let’s take an example:

@Test
public void givenStringData_whenUsingTokenizer_thenTokenizeAndValidate() {
    String data = "name=John age=30 city=NewYork";
    StringTokenizer tokenizer = new StringTokenizer(data);

    // Create a map to store key-value pairs
    Map<String, String> keyValueMap = new HashMap<>();

    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        String[] keyValue = token.split("=");

        if (keyValue.length == 2) {
            String key = keyValue[0];
            String value = keyValue[1];

            // Store key-value pairs in the map
            keyValueMap.put(key, value);
        }
    }

    // Use assertions to validate the key-value pairs in the map
    assertEquals("John", keyValueMap.get("name"));
    assertEquals("30", keyValueMap.get("age"));
    assertEquals("NewYork", keyValueMap.get("city"));
}

In this example, the input string data and the default delimiter, a space, are specified when creating a StringTokenizer object. Then, after iterating through the tokens, we separate each token into key-value pairs by using the equals symbol (=) as the delimiter.

3. Using Regular Expressions

Regular expressions with the Pattern and Matcher classes are another method for dividing a string into key-value pairs. Fortunately, this approach offers additional versatility when handling various delimiters and patterns.

Let’s take an example:

@Test
public void givenDataWithPattern_whenUsingMatcher_thenPerformPatternMatching() {
    String data = "name=John,age=30;city=NewYork";
    Pattern pattern = Pattern.compile("\\b(\\w+)=(\\w+)\\b");
    Matcher matcher = pattern.matcher(data);

    // Create a map to store key-value pairs
    Map<String, String> keyValueMap = new HashMap<>();

    while (matcher.find()) {
        String key = matcher.group(1);
        String value = matcher.group(2);

        // Store key-value pairs in the map
        keyValueMap.put(key, value);
    }

    // Use assertions to validate the key-value pairs in the map
    assertEquals("John", keyValueMap.get("name"));
    assertEquals("30", keyValueMap.get("age"));
    assertEquals("NewYork", keyValueMap.get("city"));
}

In this example, we use the Pattern class to generate a regular expression pattern like \b(\\w+)=(\\w+)\b that serves to locate and extract key-value pairs within a text. Additionally, it identifies patterns where a key, consisting of letters, digits, or underscores, is followed by an equal sign ‘=’, capturing the associated value, which similarly comprises letters, digits, or underscores.

Note that the \b markers ensure that complete key-value pairs are found, making this regex useful for parsing structured data from a given string in the “key=value” format.

Then, using the input string, we utilize a Matcher to locate and extract these pairs.

4. Using Java Streams

We can use Java Sreams to break text into key-value pairs cleanly if we use Java 8 or later.

Let’s take an example:

@Test
public void givenStringData_whenUsingJavaMap_thenSplitAndValidate() {
    String data = "name=John age=30 city=NewYork";
    Map<String, String> keyValueMap = Arrays.stream(data.split(" "))
      .map(kv -> kv.split("="))
      .filter(kvArray -> kvArray.length == 2)
      .collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));

    assertEquals("John", keyValueMap.get("name"));
    assertEquals("30", keyValueMap.get("age"));
    assertEquals("NewYork", keyValueMap.get("city"));
}

In this example, we use a space as the delimiter to divide the input string into an array of key-value pairs. Then, we further divide each pair using the equals symbol (=) by using the map procedure. Finally, we remove any pairings that do not include exactly two elements and compile the remaining pairs into a Map with associated keys and values.

5. Conclusion

Java streams, StringTokenizer, and regular expressions are only a few techniques for separating a Java string into key-value pairs.

Our needs and the intricacy of the data format we’re working with will determine our chosen solution. By being aware of these strategies, we may efficiently extract and handle data stored in key-value pairs within our Java programs.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.