Use Java programs to sort and stitch strings

Source: Internet
Author: User

1. The code program that initially generates the string:

Package Com.map.test;import Java.util.arraylist;import Java.util.collections;import java.util.hashmap;import Java.util.list;import Java.util.map;public class Test {/** * @param args */public static list<map<string, String&gt ;> maps = new arraylist<map<string,string>> (); Static{map m1 = new hashmap<string, string> (); m1.put ("Total", "ten"), M1.put ("Createtime", "2014-10-18"); Maps.add (M1); Map m2 = new hashmap<string, string> (), M2.put ("Total", "a"), M2.put ("Createtime", "2014-10-15"), Maps.add (m2); Map m3 = new hashmap<string, string> (), M3.put ("Total", "a"), M3.put ("Createtime", "2014-10-19"); Maps.add (m3); MAP M4 = new hashmap<string, string> (), M4.put ("Total", "Max"), M4.put ("Createtime", "2014-10-17"); Maps.add (M4); Map M5 = new hashmap<string, string> (), M5.put ("Total", "n"), M5.put ("Createtime", "2014-10-16"), Maps.add (M5); Map M6 = new hashmap<string, string> () m6.put ("Total", "Max"), M6.put ("Createtime", "2014-10-20"); Maps.add (M6); Map M7 = new Hashmap<string, String> (), M7.put ("Total", "Createtime"), M7.put ("The", "the", "2014-10-21"); Maps.add (M7);} 

2, Main method call stitching method

public static void Main (string[] args) {//TODO auto-generated method Stubsortlist (maps); gettimestring (); gettotalstring ();}

3. Sort the program

Package Com.map.test;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Comparator ; Import Java.util.date;import Java.util.map;public class Reportcompare implements Comparator<map<string, String >> {@Overridepublic int compare (map<string, string> O1, map<string, string> O2) {String o1time = O1.get ( "Createtime"); String o2time = O2.get ("Createtime"); SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); try {Date o1d = Sdf.parse (o1time);D ate o2d = Sdf.parse (o2time); if (O1d.before (o2d)) {return-1;} else if (o1d.equals (o2d)) {return 0;} else if (O1d.after (o2d)) {return 1;}} catch (ParseException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return 2;}}

4. Stitching strings

public static void Gettimestring () {stringbuffer sb = new StringBuffer ("["); for (map<string, string> map:maps) {SB . Append ("'" +map.get ("createtime") + "',");} Sb.deletecharat (Sb.length ()-1); Sb.append ("]"); System.out.println (Sb.tostring ());} public static void Gettotalstring () {stringbuffer sb = new StringBuffer ("["); for (map<string, string> map:maps) {s B.append (Map.get ("total") + ",");} Sb.deletecharat (Sb.length ()-1); Sb.append ("]"); System.out.println (Sb.tostring ());} public static list<map<string, string>> sortlist (list<map<string, string>> maps) { Collections.sort (Maps, new Reportcompare ()); return maps;}}

Use Java programs to sort and stitch strings

Related Article

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.