There is a line for displaying a splitter in google collections

I am looking for the opposite side of the map collector in google collections. is there something like this and if not why?

EDIT: here is an example how I would like:

Map<String,String> myMap = Splitter.on(",").keyValueSeparator("=").split("k1=v1,k2=v2"); 

EDIT: I opened the request and was implemented. will be available in guava R10.

+6
java string guava map
source share
4 answers

I think there is no such possibility in guava. For me, the reason is that a String entry can have many formats, so you need to create your own parser for your own format. An alternative (ugly) could be this if you can change the format of your input line:

 final String toSplit = "k1=v1" + getProperty("line.separator") + "k2=v2"; final Properties properties = new Properties(); properties.load(new ByteArrayInputStream(toSplit.getBytes(Charsets.UTF_8))); Maps.fromProperties(properties); 
+2
source share

Here is the Maps2 class that provides the method

 Map<String, String> mapSequence(String) 

It also provides two overloaded methods in which you can change the delimiters that are used a) between keys and values ​​(default: = ) and b) between records (default:,). Guava classes such as Splitter and Iterables are used internally to do the job. The returned display is LinkedHashMap , so the recording order is preserved.

 public final class Maps2{ public static final String DEFAULT_ENTRY_DELIMITER = ","; public static final String DEFAULT_KEYVALUE_DELIMITER = "="; private Maps2(){} public static Map<String, String> mapSequence(final String sequence){ return mapSequence(sequence, DEFAULT_KEYVALUE_DELIMITER); } public static Map<String, String> mapSequence(final String sequence, final String keyValueDelim){ return mapSequence(sequence, keyValueDelim, DEFAULT_ENTRY_DELIMITER); } public static Map<String, String> mapSequence(final String sequence, final String keyValueDelim, final String entryDelim){ final Splitter entrySplitter = Splitter.on(entryDelim) .trimResults(); final Splitter keyValueSplitter = Splitter.on(keyValueDelim) .trimResults(); final Map<String, String> map = Maps.newLinkedHashMap(); for(final String token : entrySplitter.split(sequence)){ final String[] items = Iterables.newArray( keyValueSplitter.split(token), String.class); if(items.length != 2){ throw new IllegalArgumentException( "Map String not well-formed"); } map.put(items[0], items[1]); } return map; } } 

Test code:

 public static void main(final String[] args){ // note the randomly spread whitespace in the test code, // also the last entry has no value. // using Splitter().trimResults() we can handle junk like that final Map<String, String> map = Maps2.mapSequence("k1=v1 ,k2=v2, k3 ="); System.out.println(map); } 

Output:

{k1 = v1, k2 = v2, k3 =}

+5
source share

This is pretty trivial to roll on your own:

 // assumes that input is properly formed eg "k1=v1,k2=v2" public Map<String, String> toMap(String input) { Map<String, String> map = new HashMap<String, String>(); String[] array = input.split(","); for (String str : array) { String[] pair = str.split("="); map.put(pair[0], pair[1]); } return map; } 
+1
source share

Not in the guava.

But StringToMap can do this for you.

+1
source share

All Articles