J2SE5.0 new feature automatic packing solution

xiaoxiao2021-03-06  52

J2SE5.0 new feature automatic packing solution

晁 晁 攀 Smallnest@163.com

As a programmer, a Java programmer, often encounters the following situation when using Map and Collection:

Map Map;

...

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

...

The value taken from the MAP object is required to force the conversion to what we need, and this situation is often encountered when reading attribute values ​​in JSP.

Jesus, why don't you transfer to me into the type I need?

We now automatically implement boxes and solutions. Look at the example below:

Package com.kuaff.jdk5; import java.util.date; import java.util.hashmap; import java.util.map; public class boxing {private map map = new hashmap () 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 declare a String, Date type MAP, using English should be (Map of string, Date) so we can directly pack Map.Put ("Date", new date (); reconciliation Date Date = MAP .get ("date");

This function can do not have a lot, at least we can do some letters, love our keyboard.

转载请注明原文地址:https://www.9cbs.com/read-83313.html

New Post(0)