CodeGym /జావా బ్లాగ్ /యాదృచ్ఛికంగా /జావాలో HashMap కంప్యూట్IfAbsent() పద్ధతి
John Squirrels
స్థాయి
San Francisco

జావాలో HashMap కంప్యూట్IfAbsent() పద్ధతి

సమూహంలో ప్రచురించబడింది
Java 8, మనలో చాలా మందికి తెలిసినట్లుగా, Stream APIని పరిచయం చేసింది. ఇది చాలా ఉపయోగకరమైన టూల్‌కిట్, కానీ దీనికి ఒక లోపం ఉంది, ఇది మ్యాప్‌లను కలిగి ఉండదు . అయినప్పటికీ, జావా 8 "చెడు" కోడ్ మొత్తాన్ని తగ్గించడానికి మ్యాప్ ఇంటర్‌ఫేస్‌కు అనేక ఉపయోగకరమైన పద్ధతులను జోడించింది . కాబట్టి, మీరు మ్యాప్‌లోని విలువతో కొంత చర్యను చేయవలసి వస్తే , అది దానిలో ఉన్నట్లయితే, జావా మ్యాప్‌లో ComputeIfPresent() పద్ధతి ఉంది. మీరు మ్యాప్‌లో లేని విలువతో ఏదైనా చేయాలనుకుంటే , మీరు ComputeIfAbsent() పద్ధతిని ఉపయోగించవచ్చు . మేము దానిని ఈ వ్యాసంలో పరిశీలిస్తాము.

ComputeIfAbsent() పద్ధతి సంతకం


default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
Map ( మరియు HashMap ) computeIfAbsent() పద్ధతి రెండు పారామితులను తీసుకుంటుంది. మొదటి పరామితి కీ. రెండవ పరామితి మ్యాపింగ్ ఫంక్షన్ . ఈ పద్ధతిలో మ్యాపింగ్ అందించబడకపోతే మాత్రమే మ్యాపింగ్ ఫంక్షన్ అంటారు.

ComputeIfAbsent() పద్ధతి ఎలా పని చేస్తుంది

మనకు ఇప్పటికే తెలిసినట్లుగా, Map.computeIfAbsent() పద్ధతి రెండు పారామీటర్‌లను ఆమోదించింది, ఈ కీ మ్యాపింగ్ ఫంక్షన్ కోసం విలువను లెక్కించడానికి కీ మరియు ఫంక్షన్ . పద్ధతి యొక్క తార్కిక అల్గోరిథం ఇక్కడ ఉంది:
  1. పాస్ చేసిన కీ మా మ్యాప్‌లో ప్రాతినిధ్యం వహిస్తుందో లేదో మొదట పద్ధతి తనిఖీ చేస్తుంది .
  2. మ్యాప్‌లో కీ సూచించబడితే (మరియు అది శూన్యం కాదు), అప్పుడు పద్ధతి ఏమీ చేయదు.
  3. ఒకవేళ మ్యాప్‌లో కీ ప్రాతినిధ్యం వహించకపోతే (లేదా అది శూన్యం) ఈ పద్ధతి కీకి మ్యాపింగ్ ఫంక్షన్‌ని ఉపయోగించి విలువను గణిస్తుంది .
  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() పద్ధతి బహుశా మీకు ఏదో గుర్తుచేస్తుంది. మరింత సంక్లిష్టమైన పార్సింగ్ ఉదాహరణను పరిశీలిద్దాం. మొదటి సందర్భంలో విలువ మారుతుందని నిర్ధారించుకోవడానికి కంప్యూట్ఇఫ్అబ్సెంట్() పద్ధతిని రెండుసార్లు పిలుద్దాం , రెండవ సందర్భంలో అది మారదు.

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