Java drip: Map and string conversion public methods __java

Source: Internet
Author: User
Tags stringbuffer

A simple map and string conversion method, a more practical section of Java code

1 convert map to a string like username ' Chenziwen^password ' 1234

/**
 * Method Name: transmaptostring
 * Incoming parameter: Map
 * return value: String shape such as username ' Chenziwen^password ' 1234 *
public static String transmaptostring (map map) {
  java.util.Map.Entry Entry;
  StringBuffer sb = new StringBuffer ();
  for (Iterator iterator = Map.entryset (). iterator (); Iterator.hasnext ();)
  {
    entry = (java.util.Map.Entry) Iterator.next ();
      Sb.append (Entry.getkey (). toString ()). Append ("'). Append (Null==entry.getvalue ()?" ":
      Entry.getvalue (). ToString ()). Append (Iterator.hasnext ()? "^" : "");
  }
  return sb.tostring ();
}

2 Convert a string shape such as username ' Chenziwen^password ' 1234 into a map

/** * Method Name: Transstringtomap * Incoming parameter: mapstring shape such as username ' Chenziwen^password ' 1234 * return value: MAP * *
  public static Map Transstringtomap (String mapstring) {map map = new HashMap ();
  Java.util.StringTokenizer items; 
    for (StringTokenizer Entrys = new StringTokenizer (mapstring, "^"); Entrys.hasmoretokens (); Map.put (Items.nexttoken (), Items.hasmoretokens ()?
  (Object) (Items.nexttoken ()): null) items = new StringTokenizer (Entrys.nexttoken (), "'");
return map; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.