Automatic Packing solution box for new characteristics of j2se5.0

Source: Internet
Author: User
Automatic Packing solution box for new characteristics of j2se5.0

Shi smallnest@163.com



As a programmer, a Java programmer, when using map and collection, often comes back to the following situations:

Map map;

...

String name = Map.get ("name");

...

The values fetched here from the map object need to be cast to the type we need, which is often encountered when reading property values in a JSP.



Jesus, why don't you just turn me into the type I need.

Through paradigm programming and improvements to the Java language, we now automatically implement boxing and unpacking operations. Look at the following example:

Package com.kuaff.jdk5;



Import Java.util.Date;

Import Java.util.HashMap;

Import Java.util.Map;



public class Boxing

{

Private map<string,date> Map = new hashmap<string,date> ();



Public Boxing ()

{

Map.put ("Date", new Date ());

}

public void PrintDate ()

{

Date date = Map.get ("date");

System.out.printf ("Current time:%s", date);

}



public static void Main (string[] args)

{

Boxing box = new Boxing ();

Box.printdate ();

}

}


We have declared a string,date type of map, in English it should be (Map of string,date) so that we can directly box Map.put ("Date", new Date ()), and the reconciliation case date = Map.get (" Date ");

This function may not be very big, at least we can knock down a few letters, take care of our keyboard.

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.