The Computeifabsent method in map
The implementation classes of the map interface, such as hashmap,concurrenthashmap,hashtable, inherit this method, which allows you to make your code more concise under specific requirements.
First, case description
1. Overview
In the JAVA8 map interface, a method computeifabsent is added, and this method is signed as follows:
Public Super extends
This method first determines whether the value of the specified key exists in the cache map, and if it does not, it automatically calls Mappingfunction (key) to calculate key's value and then puts key = value into the cache map.
If the value returned by Mappingfunction (key) is null or throws an exception, no record is stored in the map
2. Code Description
Public classJava8map { PublicMap<string,object> map1=Maps.newhashmap (); PublicMap<string,atomicinteger> map2=Maps.newhashmap (); PublicMap<string,list<string>> map3=Maps.newhashmap (); List<String> list = Lists.newarraylist ("1", "2", "3", "2", "3", "2"); /*** 1, business logic: If the value of key is NULL, the key is placed in the map and the corresponding value value is set */@Test Public voidMap1 () {//before Java8, getting the value from the map based on key may have the following actionObject key = Map1.get ("Key"); if(Key = =NULL) {Key=NewObject (); Map1.put ("Key", key); }Else{ //if the value of the key corresponds to the existence, the corresponding operation } //Java8, the above operation can be simplified to one line, if the key corresponding to the value of NULL, the second parameter will be stored in the return value and returnObject Key2 = map1.computeifabsent ("Key1", K-NewObject ()); System.out.println (MAP1); //output: {[email protected], [email protected]} } /*** 2, Statistics list the number of the same string */@Test Public voidmap2 () {//Although there is only one line of code, but the amount of information is very large, first it uses a new feature of Java lambda expression to traverse the list collection//This means that if the value of the key in the map corresponds to NULL, the key corresponds to the value of new Atomicinteger () and executes from 1, and if key already exists, the direct value value is incremented by 1List.foreach (str-> map2.computeifabsent (str, kNewAtomicinteger ()). Incrementandget ()); System.out.println (MAP2); //output: {1=1, 2=3, 3=2} } /*** 3, if the key corresponding to the value does not exist, then create a new list and put the data, there will be directly to the list into the data */@Test Public voidmap3 () {map3.computeifabsent ("Zhangsan", K-, Genvalue (k)). Add ("Apple"); Map3.computeifabsent ("Zhangsan", K-, Genvalue (k)). Add ("Orange"); Map3.computeifabsent ("Zhangsan", K-, Genvalue (k)). Add ("Pear"); Map3.computeifabsent ("Zhangsan", K-, Genvalue (k)). Add ("Banana"); Map3.computeifabsent ("Lisi", K-, Genvalue (k)). Add ("Water"); System.out.println (MAP3); //Output Result: {Lisi=[water], zhangsan=[apple, orange, pear, banana]} } StaticList<string>genvalue (String str) {return NewArraylist<string>(); } }
Summary : Computeifabsent In some practical development scenarios, can let our code look more concise, code quality see also higher.
The next step is really good. It knows what type of value is in my map and can adjust the corresponding method, such as this is the Atomicinteger object, then you can adjust the Incrementandget () method
If you put a collection, you can adjust the set of related methods.
List.foreach (str-> map2.computeifabsent (str, K-New Atomicinteger ()). Incrementandget ());
I just occasionally calm down and ponder over all the past. It's not worth condemning those old times that have been naïve and dull. After all, the days ahead are still long. Keep encouraging yourself,
The day is bright, is a new beginning, but also the Unknown Journey (Colonel 1)
"The beauty of Java code"---The Computeifabsent method in Java8 map