java - Convert string representing key-value pairs to Map -



java - Convert string representing key-value pairs to Map -

how can convert string map:

map m = convert("a=4 h=x po=87"); // what's convert? system.err.println(m.getclass().getsimplename()+m);

expected output:

hashmap{a=4, h=x, po=87}

there no need reinvent wheel. google guava library provides splitter class.

here's how can utilize along test code:

package com.sandbox; import com.google.common.base.splitter; import org.junit.test; import java.util.map; import static org.junit.assert.assertequals; public class sandboxtest { @test public void testquestioninput() { map<string, string> map = splittomap("a=4 h=x po=87"); assertequals("4", map.get("a")); assertequals("x", map.get("h")); assertequals("87", map.get("po")); } private map<string, string> splittomap(string in) { homecoming splitter.on(" ").withkeyvalueseparator("=").split(in); } }

java dictionary

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -