In projects, we often need to convert the received string into the corresponding collection class to save, or convert the collection class into a string for easy transmission. This tool class encapsulates several commonly used methods, which is very 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 { /** * Convert strings to set collection class* The delimiter is any whitespace character*/ public static Set<String> parseParameterList(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+]");//Match any whitespace character result.addAll(Arrays.asList(tokens)); } return result; } /** * Convert the collection into a string of the specified form*/ public static String formatParameterList(Collection<String> value) { return value == null ? null : StringUtils.collectionToDelimitedString(value, ",");//Specify the delimiter} /** * Extract the required key-value pair from the string of the query and store it in the map* The form of query 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(key).toString()); } } return map; } /** * Compare whether two sets are equal*/ public static boolean containsAll(Set<String> target, Set<String> members) { target = new HashSet<String>(target); target.retainAll(members);//Get the intersection of two sets return target.size() == members.size(); }}The above brief discussion on the tool class that commonly used string and collection conversion is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.