Splitter withKeyValueSeparator(char separator) method | Guava | Java

The withKeyValueSeparator(char separator) method of Guava’s Splitter Class accepts a mandatory parameter separator and splits entries into keys and values using this separator.

Syntax:

public Splitter.MapSplitter withKeyValueSeparator(char separator)

Parameters: The method accepts a parameter separator and splits entries into keys and values using this separator.

Return Value: The method returns a MapSplitter which splits entries based on this splitter.



Note: We pass the separator within pairs to this method. The on() method receives the separator between pairs of keys and values.

Below examples illustrate the implementation of withKeyValueSeparator() method of Guava’s Splitter class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// withKeyValueSeparator(char separator) method
// of Guava's Splitter Class
  
import com.google.common.base.Splitter;
import java.util.Map;
import java.util.Map.Entry;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // This String contains key-value pairs.
        String value
            = "Data Structures=Coding,Sports=Football,Capital=Delhi";
  
        // Using Splitter to parse key-value separators.
        // This returns a MapSplitter which splits
        // entries based on this splitter, and splits
        // entries into keys and
        // values using the specified separator.
        Map<String, String>
            mp
            = Splitter.on(',')
                  .withKeyValueSeparator('=')
                  .split(value);
  
        // Looping over entries stored in map mp.
        for (Entry<String, String> entry : mp.entrySet()) {
  
            // Displaying key and value pairs
            System.out.println(entry.getKey() + " -> "
                               + entry.getValue());
        }
    }
}

chevron_right


Output:

Data Structures -> Coding
Sports -> Football
Capital -> Delhi

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// withKeyValueSeparator(char separator) method
// of Guava's Splitter Class
  
import com.google.common.base.Splitter;
import java.util.Map;
import java.util.Map.Entry;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // This String contains key-value pairs.
        String value
            = "First=I.Second=II.Third=III.Fourth=IV";
  
        // Using Splitter to parse key-value separators.
        // This returns a MapSplitter which splits
        // entries based on this splitter, and splits
        // entries into keys and values
        // using the specified separator.
        Map<String, String>
            mp
            = Splitter.on('.')
                  .withKeyValueSeparator('=')
                  .split(value);
  
        // Looping over entries stored in map mp.
        for (Entry<String, String> entry : mp.entrySet()) {
  
            // Displaying key and value pairs
            System.out.println(entry.getKey() + " -> "
                               + entry.getValue());
        }
    }
}

chevron_right


Output:

First -> I
Second -> II
Third -> III
Fourth -> IV


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.