In the project, we often need to convert the received string to the corresponding collection class to save, or to convert the collection class to a string for easy transmission, this tool class encapsulates a few common methods, for this conversion requirements is very convenient.
1 Importjava.util.Arrays;2 Importjava.util.Collection;3 ImportJava.util.HashMap;4 ImportJava.util.HashSet;5 ImportJava.util.Map;6 Importjava.util.Properties;7 ImportJava.util.Set;8 ImportJava.util.TreeSet;9 Ten Public classMystringutils { One A /** - * Converts a string into a set collection class - * Delimiter is any white space character the */ - Public StaticSet<string>parseparameterlist (String values) { -set<string> result =NewTreeset<string>(); - if(Values! =NULL&& Values.trim (). Length () > 0) { + //The spec says the scope is separated by spaces -string[] tokens = Values.split ("[\\s+]");//match any white space character + Result.addall (Arrays.aslist (tokens)); A } at returnresult; - } - - /** - * Convert a set into a string of a specified form - */ in Public StaticString Formatparameterlist (collection<string>value) { - returnValue = =NULL?NULL: stringutils.collectiontodelimitedstring (Value, ",");//Specify delimiter to } + - /** the * Extract the required key-value pairs from the query string into the map * * form of query Name=god&password=111&method=up $ */Panax Notoginseng Public StaticMap<string, string>extractmap (String query) { -map<string, string> map =NewHashmap<string, string>(); theProperties Properties =Stringutils.splitarrayelementsintoproperties ( +Stringutils.delimitedlisttostringarray (Query, "&"), "="); A if(Properties! =NULL) { the for(Object key:properties.keySet ()) { + Map.put (key.tostring (), Properties.get (key). ToString ()); - } $ } $ returnmap; - } - the /** - * Compare whether two sets are equalWuyi */ the Public Static BooleanContainsall (set<string> target, set<string>Members ) { -target =NewHashset<string>(target); WuTarget.retainall (members);//take the intersection of two sets - returnTarget.size () = =members.size (); About } $}
Tool classes for commonly used string and collection class conversions