Takes a query string and separates the constituent name-value pairs, and stores them in a SortedMap ordered by lexicographical order. - Java java.util

Java examples for java.util:Map Value

Description

Takes a query string and separates the constituent name-value pairs, and stores them in a SortedMap ordered by lexicographical order.

Demo Code


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main{
    public static void main(String[] argv){
        String queryString = "java2s.com";
        System.out.println(queryStringToMap(queryString));
    }//from  w ww  . java2 s  . c o m
    /**
     * Takes a query string and separates the constituent name-value pairs, and stores
     * them in a SortedMap ordered by lexicographical order.
     *
     * @return Null if there is no query string.
     */
    public static SortedMap<String, String> queryStringToMap(
            final String queryString) {
        if (queryString == null || queryString.isEmpty()) {
            return null;
        }

        final String[] pairs = queryString.split("&");
        final Map<String, String> map = new HashMap<String, String>(
                pairs.length);

        for (final String pair : pairs) {
            if (pair.length() < 1) {
                continue;
            }

            String[] tokens = pair.split("=", 2);
            for (int j = 0; j < tokens.length; j++) {
                try {
                    tokens[j] = URLDecoder.decode(tokens[j], "UTF-8");
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(CollectionUtils.class.getName()).log(
                            Level.SEVERE, null, ex);
                }
            }
            switch (tokens.length) {
            case 1: {
                if (pair.charAt(0) == '=') {
                    map.put("", tokens[0]);
                } else {
                    map.put(tokens[0], "");
                }
                break;
            }
            case 2: {
                map.put(tokens[0], tokens[1]);
                break;
            }
            }
        }

        // Convert to TreeMap so the keys are sorted.
        return new TreeMap<String, String>(map);
    }
}

Related Tutorials