In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Java8 in how to use compute and merge methods, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Basic data:
KeyAndValue A1 = new KeyAndValue (). SetName ("kevin"). SetValue ("lee"); KeyAndValue a5 = new KeyAndValue (). SetName ("kevin2"). SetValue ("lee5"); KeyAndValue A6 = new KeyAndValue (). SetName ("kevin3"). SetValue ("lee8")
The method of scenario 1 correspondence is the compute method.
Compute, processes the element that exists in key and returns the processed element, which is also replaced with the processed element in map. If the value is calculated or logically processed, the value must be guaranteed to be non-null. Examples are as follows:
Map maps = new HashMap (); maps.put ("a", A1); System.out.println (maps); KeyAndValue compute1 = maps.compute in the presence of {a=KeyAndValue (name=kevin, value=lee)} / / key ("a", (k, v)-> {Optional.ofNullable (v) .ifPresent (v1-> v1.setValue (v.getValue (). Concat ("_ ddddd")); return v;}); System.out.println (compute1) / / KeyAndValue (name=kevin, value=lee__ddddd) System.out.println (maps); / / {a=KeyAndValue (name=kevin, value=lee__ddddd)} / / KeyAndValue compute = maps.compute ("b", (k, v)-> {Optional.ofNullable (v) .ifPresent (v1-> v1.setValue (v.getValue (). Concat ("_ ddddd")); return v;}); System.out.println (compute); / / nullSystem.out.println (maps) / / {a=KeyAndValue (name=kevin, value=lee__ddddd)}
Of course, there are targeted methods (computeIfAbsent and computeIfPresent) for the non-existence and existence of value.
ComputeIfAbsent
If key does not exist or null, then process, return the processed result, and update value
/ / key does not exist or nullMap map = new HashMap (); System.out.println (map); / / {} KeyAndValue computeIfAbsent = map.computeIfAbsent ("a", s-> {return a6;}); System.out.println (computeIfAbsent); / / KeyAndValue (name=kevin3, value=lee8) System.out.println (map); / / {a=KeyAndValue (name=kevin3, value=lee8)}
If key exists, it will not be processed, value will be returned and map will not be updated. Examples are as follows:
/ / key exists. Without any processing, valueMap map = new HashMap (); map.put ("a", A1); System.out.println (map); / / {a=KeyAndValue (name=kevin, value=lee)} KeyAndValue computeIfAbsent1 = map.computeIfAbsent ("a", s-> {return a6;}); System.out.println (computeIfAbsent1); / / KeyAndValue (name=kevin, value=lee) System.out.println (map); / / {a=KeyAndValue (name=kevin, value=lee)}
ComputeIfPresent
Process if key exists, return the result after processing, and update value
/ / key presence deals with Map map = new HashMap (); map.put ("a", A1); System.out.println (map); / / {a=KeyAndValue (name=kevin, value=lee)} KeyAndValue computeIfPresent = map.computeIfPresent ("a", (k, v)-> {return v.setValue (v.getValue (). Concat ("_ 00000"));}); System.out.println (computeIfPresent); / / KeyAndValue (name=kevin, value=lee_00000) System.out.println (map); / / {a=KeyAndValue (name=kevin, value=lee_00000)}
If key does not exist or null does not exist, it will not be processed. Null will be returned and value will not be updated.
/ / process Map map = new HashMap () if key does not exist; / / map.put ("a", null); System.out.println (map); / / {} KeyAndValue computeIfPresent = map.computeIfPresent ("a", (k, v)-> {return v.setValue (v.getValue (). Concat ("_ 00000");}); System.out.println (computeIfPresent); / / nullSystem.out.println (map); / / {}
Merge method corresponding to scenario 2
Merge: merge the elements that exist in key and return the processed elements
A6 is the new value combined. A6 and newV are the same object, so newV and A6 are interchangeable.
OldV is the corresponding value of the original key in map. If oldV has a value, it will be merged. If there is no value, the corresponding key-value pair of k and value=newV will be added.
Map map = new HashMap (); map.put ("a", A1); System.out.println (map); / / {a=KeyAndValue (name=kevin, value=lee)} / / "a" has a value, then merge KeyAndValue merge = map.merge ("a", A6, (oldV, newV)-> {oldV.setValue (newV.getValue () + "_ _" + oldV.getValue (); return oldV;}); System.out.println (merge) / / KeyAndValue (name=kevin, value=lee8___lee) System.out.println (map); / / {a=KeyAndValue (name=kevin, value=lee8___lee)} / / "b" has no value, then add "b": newVKeyAndValue merge1 = map.merge ("b", A5, (oldV, newV)-> {oldV.setValue (oldV.getValue () + "_ _" + oldV.getValue ()); return oldV / / A5 is the new value combined. A5 and newV are the same object, so newV and A5 are interchangeable. / / oldV is the value corresponding to the original key= "b" in map. If oldV has a value, it will be merged; if there is no value, it will be added}); System.out.println (merge1); / / KeyAndValue (name=kevin2, value=lee5) System.out.println (map); / / {a=KeyAndValue (name=kevin, value=lee), b=KeyAndValue (name=kevin2, value=lee5)} will it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
WebDriver API online API: http://selenium-python.readthedocs.io/waits.html
© 2024 shulou.com SLNews company. All rights reserved.