In projects, 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, which encapsulates several common methods that are convenient for this conversion requirement.
Import Java.util.Arrays;
Import java.util.Collection;
Import Java.util.HashMap;
Import Java.util.HashSet;
Import Java.util.Map;
Import java.util.Properties;
Import Java.util.Set;
Import Java.util.TreeSet; public class Mystringutils {/** * Converts a string to a set collection class * separator is any whitespace character/public static set<string> Parseparame
Terlist (String values) {set<string> result = new treeset<string> ();
If values!= null && values.trim (). Length () > 0) {//The spec says the scope is separated by spaces
string[] tokens = Values.split ("[\\s+]");//matches any whitespace character result.addall (arrays.aslist (tokens));
return result; /** * Converts the collection to a specified form of String */public static string Formatparameterlist (collection<string> value) {return Value = null? Null:StringUtils.collectionToDelimitedString (Value, ",");//Specify Delimiter}/** * Extract the required key value from query string to deposit in map * Query form Name=god&password=111&method=up * * public static Map<striNg, string> extractmap (String query) {map<string, string> Map = new hashmap<string, string> (); Properties Properties = Stringutils.splitarrayelementsintoproperties (stringutils.delimitedlisttostringarray) (query
, "&"), "="); if (properties!= null) {for (Object Key:properties.keySet ()) {Map.put (key.tostring (), Properties.get (ke
y). toString ());
} return map;
/** * Compare two sets for equality */public static Boolean Containsall (set<string> target, set<string> members) {
target = new hashset<string> (target);
Target.retainall (members)//Take two sets of intersection return target.size () = = Members.size (); }
}
The above this article discusses the common string and the collection class conversion tool class is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.