Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaSplitter withKeyValueSeparator(char separator) method | Guava | Java

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:




// 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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments