Skip to content
Related Articles

Related Articles

Improve Article

Splitter withKeyValueSeparator(char separator) method | Guava | Java

  • Last Updated : 31 Jan, 2019

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:




// 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());
        }
    }
}
Output:
Data Structures -> Coding
Sports -> Football
Capital -> Delhi

Example 2:




// 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());
        }
    }
}
Output:
First -> I
Second -> II
Third -> III
Fourth -> IV

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :