CodeGym /مدونة جافا /Random-AR /طريقة HashMap computeIfAbsent () في Java
John Squirrels
مستوى
San Francisco

طريقة HashMap computeIfAbsent () في Java

نشرت في المجموعة
Java 8، كما يعلم الكثير منا، قدم Stream API. هذه مجموعة أدوات مفيدة جدًا، ولكن لها عيبًا، فهي لا تتضمن Maps . ومع ذلك، أضاف Java 8 عدة طرق مفيدة إلى واجهة الخريطة نفسها لتقليل كمية التعليمات البرمجية "السيئة". لذلك، إذا كنت بحاجة إلى تنفيذ بعض الإجراءات باستخدام القيمة الموجودة في الخريطة ، إذا كانت موجودة فيها، فهناك طريقة ComputeIfPresent() في Java Map. إذا كنت بحاجة إلى القيام بشيء ما بقيمة غير موجودة في Map ، فيمكنك استخدام طريقة ComputeIfAbsent() . سننظر فيها في هذا المقال.

توقيع أسلوب computeIfAbsent()

default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
تأخذ طريقة Map (و HashMap ) computeIfAbsent () معلمتين. المعلمة الأولى هي المفتاح. المعلمة الثانية هي mappingFunction . في هذه الطريقة، يتم استدعاء وظيفة التعيين فقط في حالة عدم تقديم التعيين.

كيف تعمل طريقة computeIfAbsent()

كما نعلم بالفعل، يتم تمرير أسلوب Map.computeIfAbsent() إلى معلمتين، المفتاح والوظيفة لحساب قيمة هذا المفتاح mappingFunction . فيما يلي الخوارزمية المنطقية للطريقة:
  1. تتحقق الطريقة أولاً مما إذا كان المفتاح الذي تم تمريره ممثلاً في خريطتنا .
  2. إذا تم تمثيل المفتاح في الخريطة (وليس فارغًا)، فلن تفعل الطريقة شيئًا.
  3. بخلاف ذلك، إذا لم يكن المفتاح موجودًا في الخريطة (أو كان فارغًا)، فستقوم الطريقة بحساب القيمة باستخدام mappingFunction للمفتاح.
  4. إذا لم تكن القيمة الناتجة فارغة، فاكتب زوجًا من القيمة الرئيسية لتعيينه.
لنكتب نفس منطق الكود:
if (map.get(key) == null)
{
V newValue = mappingFunction.apply(key);
if (newValue != null) map.put(key, newValue);
}

مثال على كود computeIfAbsent()

لذا، إذا لم تكن القيمة موجودة في الخريطة ، فستقوم الطريقة بإجراء التغييرات. دعونا نلقي نظرة على مثال بسيط:
import java.util.HashMap;
import java.util.Map;

//map.computeIfAbsent example
public class ComputeIfAbsentExample {

       public static void main(String[] args) {

           Map<String, String> myMap = new HashMap<>();

           myMap.computeIfAbsent("here is my key", key -> key + ", " + "and this is a new value");

           System.out.println(myMap.get("here is my key"));
       }
}
الإخراج هو:
هذا هو مفتاحي، وهذه قيمة جديدة
الآن دعونا نرى ما ستفعله الطريقة عندما تكون هناك قيمة معينة في الخريطة . تنبيه المفسد: لن يفعل أي شيء.
import java.util.HashMap;
import java.util.Map;

public class ComputeIfAbsentExample2 {

       public static void main(String[] args) {

           Map<String, String> myMap = new HashMap<>();
           myMap.put("here is my key", "and here is my value");

           myMap.computeIfAbsent("here is my key", key -> key + ", " + "and this is a new value");

           System.out.println(myMap.get("here is my key"));
       }
}
هنا هو الإخراج:
وهنا قيمتي
كما ترون، تبقى القيمة دون تغيير.

مثال آخر لـ ComputeIfAbsent()

إذا كنت على دراية بمفهوم التخزين المؤقت، فمن المحتمل أن تذكرك طريقة computeIfAbsent() بشيء ما. دعونا نلقي نظرة على مثال تحليل أكثر تعقيدا. لنستدعي التابع computeIfAbsent() ‎ مرتين للتأكد من أن القيمة تتغير في الحالة الأولى، بينما لا تتغير في الحالة الثانية.
import java.util.HashMap;
import java.util.Map;

public class ComputeIfAbsentExample {
   private static Map<String, Long> numbersMap = new HashMap<>();

   public static Long stringToLong(String str) {
       return numbersMap.computeIfAbsent(str, key -> {
           System.out.println("parsing: " + key);
           return Long.parseLong(key);
       });
   }

   public static void main(String[] args) {
       // will print:
       // > parsing: 10
       // > parsing: 25
       // > 10+25=35
       System.out.println("10+25=" + (stringToLong("10") + stringToLong("25")));
       // will print:
       // > parsing: 20
       // > 10+25=45
       // only "20" will be parsed this time, since "25" was already parsed and placed into `numbersMap` map before
       System.out.println("20+25=" + (stringToLong("20") + stringToLong("25")));
       // will print:
       // > 10+20=30
       // no parsing will occur, since both "10" and "20" were already parsed and placed into `numbersMap` map before
       System.out.println("10+20=" + (stringToLong("10") + stringToLong("20")));
   }

}
هنا هو الإخراج:
التحليل: 10 التحليل: 25 10+25=35 التحليل: 20 20+25=45 10+20=30
تعليقات
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION