CodeGym /จาวาบล็อก /สุ่ม /วิธี HashMap computeIfAbsent() ใน Java
John Squirrels
ระดับ
San Francisco

วิธี HashMap computeIfAbsent() ใน Java

เผยแพร่ในกลุ่ม
Java 8 อย่างที่เราทราบกันดีว่าได้แนะนำ Stream API นี่เป็นชุดเครื่องมือที่มีประโยชน์มาก แต่มีข้อเสียคือไม่มีMap s อย่างไรก็ตาม Java 8 ได้เพิ่มวิธีการที่มีประโยชน์หลายอย่างให้กับ ส่วนต่อประสาน แผนที่เพื่อลดจำนวนโค้ดที่ "ไม่ดี" ดังนั้น หากคุณต้องการดำเนินการบางอย่างด้วยค่าในMapหากมีอยู่ในนั้น จะมี เมธอด 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. หากคีย์แสดงอยู่ในMap (และไม่ใช่ค่าว่าง) แสดงว่าเมธอดไม่ทำอะไรเลย
  3. มิฉะนั้น หากคีย์ไม่ได้แสดงในMap (หรือเป็นค่าว่าง) วิธีการจะคำนวณค่าโดยใช้mappingFunctionกับคีย์
  4. หากค่าผลลัพธ์ไม่เป็น null ให้เขียนคู่คีย์-ค่าเพื่อจับคู่
มาเขียนตรรกะเดียวกันกับโค้ด:

if (map.get(key) == null) 
{ 
V newValue = mappingFunction.apply(key); 
if (newValue != null) map.put(key, newValue); 
}

ตัวอย่างโค้ด computeIfAbsent()

ดังนั้นหากค่าไม่อยู่ในMapวิธีการจะทำการเปลี่ยนแปลง ลองมาดูตัวอย่างง่ายๆ:

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