استنساخ HashMap () ، putIfAbsent () ، computeIfAbsent () ، computeIfPresent () الأساليب في Java (أمثلة)
نشرت: 2021-05-11
في هذا البرنامج التعليمي لجافا ، سوف نستعرض التفاصيل حول كيفية استخدام طرق HashMap أدناه:
- استنساخ ()
- putIfAbsent (مفتاح K ، قيمة V)
- computeIfAbsent (مفتاح K ، وظيفة java.util.function. تعيين الوظيفة)
- computeIfPresent (مفتاح K ، java.util.function. إعادة تعيين
BiFunction
)
hashMap.clone ():
إرجاع نسخة سطحية من مثيل HashMap هذا: المفاتيح والقيم نفسها غير مستنسخة.
hashMap.putIfAbsent ():
إذا لم يكن المفتاح المحدد مقترنًا بالفعل بقيمة (أو تم تعيينه إلى قيمة خالية) ، فإنه يربطه بالقيمة المحددة ويعيد القيمة فارغة ، وإلا فإنه يُرجع القيمة الحالية.
hashMap.computeIfAbsent ():
إذا لم يكن المفتاح المحدد مقترنًا بالفعل بقيمة (أو تم تعيينه إلى قيمة خالية) ، فسيحاول حساب قيمته باستخدام وظيفة التعيين المحددة وإدخاله في هذه الخريطة ما لم يكن فارغًا.
hashMap.computeIfPresent ():
إذا كانت قيمة المفتاح المحدد موجودة وغير فارغة ، فسيحاول حساب تعيين جديد بالنظر إلى المفتاح وقيمته الحالية المعينة.
هيا بنا نبدأ:
- إنشاء فئة جافا
CrunchifyCloneHashmap
.java - ضع الرمز أدناه فيه
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package crunchify . com . java . tutorials ; import java . util . HashMap ; /** * @author Crunchify.com * HashMap's clone(), putIfAbsent(), computeIfAbsent(), computeIfPresent() Methods in Java (Example attached) */ public class CrunchifyCloneHashmap { public static void main ( String [ ] args ) { // Create our first Hashmap crunchifyHashMap HashMap < String , Integer > crunchifyHashMap = new HashMap < > ( ) ; // Add elements to HashMap crunchifyHashMap crunchifyHashMap . put ( "Crunchify" , 11 ) ; crunchifyHashMap . put ( "Apple" , 12 ) ; crunchifyHashMap . put ( "Twitter" , 13 ) ; crunchifyHashMap . put ( "Facebook" , 14 ) ; crunchifyHashMap . put ( "Chase" , 15 ) ; crunchifyPrint ( "crunchifyHashMap Details: " + crunchifyHashMap ) ; HashMap < String , Integer > crunchifyHashMapNew ; // Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned. crunchifyHashMapNew = ( HashMap < String , Integer > ) crunchifyHashMap . clone ( ) ; // If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value. crunchifyHashMapNew . putIfAbsent ( "Crunchify" , 16 ) ; crunchifyHashMapNew . putIfAbsent ( "Google" , 17 ) ; crunchifyHashMapNew . put ( "WordPress" , 18 ) ; // If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null. //If the mapping function returns null, no mapping is recorded. If the mapping function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded. The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in: // // map.computeIfAbsent(key, k -> new Value(f(k))); // //Or to implement a multi-value map, Map<K,Collection<V>>, supporting multiple values per key: // // map.computeIfAbsent(key, k -> new HashSet<V>()).add(v); crunchifyHashMapNew . computeIfAbsent ( "Paypal" , k - > 100 + 200 ) ; // If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. //If the remapping function returns null, the mapping is removed. If the remapping function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged. //The remapping function should not modify this map during computation. //This method will, on a best-effort basis, throw a ConcurrentModificationException if it is detected that the remapping function modifies this map during computation. crunchifyHashMapNew . computeIfPresent ( "Crunchify" , ( key , val ) - > 300 ) ; crunchifyPrint ( "crunchifyHashMapNew Details: " + crunchifyHashMapNew ) ; // Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), // the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via // the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations. crunchifyPrint ( "crunchifyHashMap keyset(): " + crunchifyHashMap . keySet ( ) ) ; crunchifyPrint ( "crunchifyHashMapNew keyset(): " + crunchifyHashMapNew . keySet ( ) ) ; // Returns the number of key-value mappings in this map. crunchifyPrint ( "crunchifyHashMap size(): " + crunchifyHashMap . size ( ) ) ; crunchifyPrint ( "crunchifyHashMapNew size(): " + crunchifyHashMapNew . size ( ) ) ; } private static void crunchifyPrint ( String crunchifyString ) { System . out . println ( "===> " + crunchifyString + "\n" ) ; } } |
في كود Java أعلاه ، لن ترى crunchifyHashMapNew.putIfAbsent (“Crunchify”، 16)؛ تمت الإضافة إلى HashMap الجديد.

ما عليك سوى تشغيل البرنامج أعلاه كتطبيق Java إما في Eclipse IDE أو IntelliJ IDEA.
يجب أن تشاهد نتيجة وحدة التحكم مشابهة لهذا:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/ Library / Java / JavaVirtualMachines / jdk - 15.jdk / Contents / Home / bin / java - javaagent : / Applications / IntelliJ 5.1.3.RELEASE.jar crunchify . com . java . tutorials . CrunchifyCloneHashmap === > crunchifyHashMap Details : { Apple = 12 , Twitter = 13 , Facebook = 14 , Crunchify = 11 , Chase = 15 } === > crunchifyHashMapNew Details : { Paypal = 300 , Google = 17 , Apple = 12 , WordPress = 18 , Twitter = 13 , Facebook = 14 , Crunchify = 300 , Chase = 15 } === > crunchifyHashMap keyset ( ) : [ Apple , Twitter , Facebook , Crunchify , Chase ] === > crunchifyHashMapNew keyset ( ) : [ Paypal , Google , Apple , WordPress , Twitter , Facebook , Crunchify , Chase ] === > crunchifyHashMap size ( ) : 5 === > crunchifyHashMapNew size ( ) : 8 Process finished with exit code 0 |

اسمحوا لي أن أعرف إذا كنت تواجه أي مشكلة في تشغيل الكود.