One: Introduction
In JDK1.8, Map has new methods, some of which are designed to simplify code, such as foreach, and other methods to prevent null and to make the code more rigorous. II: Map
Public interface Map<k,v> {
//If key exists, ignore put operation
Default v putifabsent (K key, V value) {
v v = Get (key); C3/>if (v = = null) {
v = put (key, value);
}
return v;
}
Loop public
void ForEach (biconsumer<. Super K,? Super V> Action) {...}
If the existence is calculated: First to determine whether the key exists, if the key exists, the result of the bifunction calculation as a new value of key to put into the public
V computeifpresent (K key, bifunction<? Super K, huh? Super V, huh? Extends V> remappingfunction) {}
//If key does not exist put the result of the calculation into public
V computeifabsent (K key, function<? Super K, huh? Extends V> mappingfunction) {}
///
The public boolean remove (object key, Object value) {} is deleted only if key-value satisfies the condition at the same time
//If key does not exist, return default value public
V getordefault (Object key, v defaultvalue) {}//
merge: The result of bifunction as the new value of key C19/>public v Merge (K key, v value, bifunction<? Super V,? Super V,? Extends V> remappingfunction) {}
Three: Example
@Test public void Putifabsent () {map<string, string> Map = new hashmap<> ();
Map.putifabsent ("Key", "OldValue");
If key exists, the put operation Map.putifabsent ("Key", "NewValue") is ignored;
String value = Map.get ("key");
System.out.println (value);
@Test public void ForEach () {map<string, string> Map = new hashmap<> ();
Map.putifabsent ("Key1", "value1");
Map.putifabsent ("Key2", "value1");
Map.putifabsent ("Key3", "value1");
Map.foreach ((key, value)-> System.out.println (key + ":" + value);}
@Test public void Computeifpresent () {map<string, string> Map = new hashmap<> ();
Map.putifabsent ("Key1", "value1"); If the existence is calculated: First to determine whether the key exists, if the key exists, the result of the bifunction calculation as a new value of key into the Map.computeifpresent ("Key1", (key, value)-> key + "=
"+ value);
String value = Map.get ("Key1");
System.out.println (value);
If the computed result is null, the equivalent of removing map.computeifpresent ("Key1", (k, v)-> null) from the map; Boolean contain = Map.containskey ("Key1");
System.out.println (contain); @Test public void Computeifabsent () {//If key does not exist put the result of the calculation in map<string, string> Map = new HASHMAP<&G
t; ();
Map.computeifabsent ("Key2", v-> "value2");
Boolean contain = Map.containskey ("Key2");
System.out.println (contain);
@Test public void Remove () {map<string, string> Map = new hashmap<> ();
Map.putifabsent ("Key1", "value1");
Boolean result = Map.Remove ("Key1", "value2");
SYSTEM.OUT.PRINTLN (result);
@Test public void Getordefault () {map<string, string> Map = new hashmap<> ();
String value = Map.getordefault ("Key1", "Default value");
System.out.println (value);
@Test public void Merge () {map<string, string> Map = new hashmap<> ();
Map.put ("Key1", "value1");
Map.merge ("Key1", "NewValue", (value, NewValue)-> value + "-" + newvalue);
String value = Map.get ("Key1"); Value1-newvalue System.out.priNtln (value); }
The Java 8 contains a new set of time and date APIs under the package Java.time. The new date API is similar to the open source Joda-time library, but it's not exactly the same. Clock Clock
The clock class provides a way to access the current date and time, clock is time zone sensitive and can be used instead of system.currenttimemillis () to get the current number of microseconds. A particular point in time can also be represented using the instant class, which can also be used to create old Java.util.Date objects.
@Test public
Void Clock () {
Clock clock = Clock.systemdefaultzone ();
Long Millis = Clock.millis ();
Instant Instant = Clock.instant ();
Date date = Date.from (instant);
System.out.println (Millis);
SYSTEM.OUT.PRINTLN (date);
timezones time Zone
In the new API, the time zone is represented using ZoneID. Time zones can be easily obtained by using the static method of. Time zones define the time difference to UTS times and are extremely important when converting between instant Point-in-time objects to local date objects.
@Test public
void TimeZones () {
//Get all available time zones
set<string> availablezoneids = Zoneid.getavailablezoneids ();
System.out.println (availablezoneids);
Gets the default time zone Asia/shanghai
zoneid ZoneID = Zoneid.systemdefault ();
System.out.println (ZoneID);
Get time zone rule zonerules[currentstandardoffset=+08:00]
zonerules rules = Zoneid.getrules ();
SYSTEM.OUT.PRINTLN (rules);
localtime local time
LocalTime defines a time with no time zone information, such as 10 o'clock in the evening, or 17:30:15.
@Test public
void LocalTime () {
ZoneID ZoneID = Zoneid.systemdefault ();
ZoneID zoneId2 = Zoneid.of ("etc/gmt+8");
Gets the current time of the specified time zone
localtime now = Localtime.now (ZoneID);
LocalTime now2 = Localtime.now (ZONEID2);
To determine whether a local time precedes another local time
boolean isbefore = Now.isbefore (now2);
Gets the difference between two local time hours
long hours = ChronoUnit.HOURS.between (now, now2);
System.out.println (hours);
Gets the difference between two local time minutes
long minutes = ChronoUnit.MINUTES.between (now, now2);
System.out.println (minutes);
LocalTime localtime = Localtime.of (in);
System.out.println (localtime); 23:59:59
DateTimeFormatter formatter = Datetimeformatter.oflocalizedtime (Formatstyle.short). WithLocale ( Locale.german);
LocalTime local = Localtime.parse ("13:37", formatter);
SYSTEM.OUT.PRINTLN (local); 13:37
}
Localdate Local Date
Localdate indicated an exact date, such as 2014-03-11. The value of the object is immutable and is basically consistent with localtime.
@Test public
void Localdate () {
localdate today = Localdate.now ();
Localdate tomorrow = Today.plus (1, chronounit.days);
Localdate yesterday = tomorrow.minusdays (2);
SYSTEM.OUT.PRINTLN (Today + "," + Tomorrow + "," + yesterday);
Localdate IndependenceDay = Localdate.of (2014, month.july, 4);
DayOfWeek DayOfWeek = Independenceday.getdayofweek ();
System.out.println (DayOfWeek);
DateTimeFormatter germanformatter = datetimeformatter.oflocalizeddate (formatstyle.medium)
. WithLocale ( Locale.german);
Localdate localdate = Localdate.parse ("24.12.2014", germanformatter);
System.out.println (localdate); 2014-12-24
}
LocalDateTime Local Date Time
LocalDateTime also represents the time and date, which is equivalent to merging the first two sections onto an object. LocalDateTime and LocalTime, as well as localdate, are immutable. LocalDateTime provides a number of ways to access specific fields.
@Test public void LocalDateTime () {LocalDateTime localDateTime = Localdatetime.of (2014, Month .
December, 31, 23, 59, 59);
DayOfWeek DayOfWeek = Localdatetime.getdayofweek ();
System.out.println (DayOfWeek);
Month Month = Localdatetime.getmonth ();
SYSTEM.OUT.PRINTLN (month);
Long Minuteofday = Localdatetime.getlong (Chronofield.minute_of_day);
System.out.println (Minuteofday);
Instant Instant = Localdatetime.atzone (Zoneid.systemdefault ()). Toinstant ();
Date date = Date.from (instant);
SYSTEM.OUT.PRINTLN (date); DateTimeFormatter is immutable, so it is thread-safe datetimeformatter formatter = Datetimeformatter.ofpattern ("Yyyy-mm-dd HH:mm:ss")
;
LocalDateTime parsed = Localdatetime.parse ("2018-05-07 07:13:00", formatter);
String string = Formatter.format (parsed);
System.out.println (string); }