Use of the Java Map List

Source: Internet
Author: User

One of the requirements in the project is to display the month and year in two lines:

Java Background Code

To create a demo, use the map List to split the month:

Package demo;

Import java.util.ArrayList;
Import java.util.List;
Import Java.util.HashMap;
Import Java.util.Map;

public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub
String S1 = "2016-10";
String s2 = "2016-11";
String s3 = "2016-12";
String S4 = "2017-1";
list<string> sList = new arraylist<string> ();
Slist.add (S1);
Slist.add (S2);
Slist.add (S3);
Slist.add (S4);
SYSTEM.OUT.PRINTLN ("Initial month SList:" + sList);

map<string,integer> yearsdic = Getyearsandcount (sList);
System.out.println ("Number of months under each year Yearsdic:" + yearsdic);

Set up year and month collections
list<string> yearlist = new arraylist<string> ();
list<string> monthlist = new arraylist<string> ();

for (int i=0; i< slist.size (); i++)
{
String year = Slist.get (i). Split ("-") [0];
String month = Slist.get (i). Split ("-") [1];
if (!yearlist.contains (year))
{
Yearlist.add (year);
}
Monthlist.add (month);
}
System.out.println ("Year set Yearlist:" + yearlist);
SYSTEM.OUT.PRINTLN ("Month set Monthlist:" + monthlist);
}

/* Returns the year collection, the number of columns per year */
private static map<string, integer> Getyearsandcount (list<string> sList)
{
map<string, integer> yearcount = new hashmap<string, integer> ();
for (int i = 0; i < slist.size (); i++)
{
String year = Slist.get (i). Split ("-") [0];
if (Yearcount.containskey (year))
{
Integer count = Yearcount.get (year);
Yearcount.put (year, count+1);//Add Column
}else {
Yearcount.put (year, 1);//Initial value 1
}
}
return yearcount;
}
}

Console output:

The project is spring4.0 and added to the view:

/**list *@paramPage *@throwsException*/@RequestMapping (Value= "/list")     PublicModelandview List (Page page)throwsexception{Modelandview MV= This. Getmodelandview (); Mv.addobject ("Months", monthlist); Mv.addobject ("Yearscount", Yearsdic); Mv.addobject ("Yearlist", yearslist); returnMV; }

Put it in the JSP display:

Use of the Java Map List

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.