Optional in Java 8 In Hindi
जावा प्रोग्रामिंग लैंग्वेज में ऑप्शनल एक यूजर डिफाइन क्लास डाटा टाइप स्ट्रक्चर है, ऑप्शनल क्लास को जावा 8 वर्जन में java.util पैकेज लाइब्रेरी में पेश किया गया है। ऑप्शनल क्लास का यूज़ मुख्य रूप से ऐसी वैल्यू यूजर डिफाइन एक कस्टम वैल्यू डिस्प्ले करने में किया जाता है, जो किसी ऑप्शनल क्लास में मौजूद हो भी सकती है, और नहीं भी सकती है।

जावा में ऑप्शनल क्लास null, स्पेशली NullPointerException से जनरेट होने वाली किसी प्रोब्लेम्स या इश्यूज को कम करने में हेल्प करता है।
import java.util.Optional;
Creating an Optional class in a Java program.
Optional.of() function in the Optional Java class.
जावा प्रोग्राम में Optional.of() फंक्शन मेथड का यूज़ तब किया जाता है, जब ऑप्शनल क्लास वैल्यू नॉन-नल डिफाइन हो।
Example of Optional.of() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.of(“Vcanhelpsu”);
System.out.println(comp_name);
}
}
its output.
Optional[Vcanhelpsu]
यदि जावा यूजर इसमें of() को null से पास करते हैं, तो यह NullPointerException को थ्रो करता है।
Optional<String> comp_name = Optional.of(null); //
Optional.ofNullable()
जावा में इस मेथड का यूज़ तब किया जाता है, जब कोई वैल्यू नल हो सकती है।
import java.util.Optional;
public class Main {
public static void main(String[] args) {
String comp_name = null;
Optional<String> output =
Optional.ofNullable(comp_name);
System.out.println(output);
}
}
its output.
Optional.empty
Optional.empty()
यह एक एम्प्टी Optional एलिमेंट को क्रिएट करता है।
Optional<String> output =
Optional.empty();
Checking whether a value is present in the Optional class.
isPresent() function Optional class in a Java program.
जावा प्रोग्राम में यदि कोई यूजर डिफाइन सर्च फाइंड डाटा वैल्यू मौजूद है, तो यह true आउटपुट को रिटर्न करता है।
Example of isPresent() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.of(“Vcanhelpsu”);
if (comp_name.isPresent()) {
System.out.println(comp_name.get());
}
}
}
its output.
Vcanhelpsu
Getting a value in the Java Optional class.
get() function in the Optional Java class.
जावा प्रोग्राम में get() फंक्शन मेथड ऑप्शनल के अंदर स्टोर की गई यूजर डिफाइन कोई भी कस्टम वैल्यू को रिटर्न करता है।
Example of get() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.of(“Vcanhelpsu”);
String output = comp_name.get();
System.out.println(output);
}
}
जैसाकी, एम्प्टी Optional पर get() फंक्शन को कॉल करने पर. NoSuchElementException थ्रो होता है।
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.empty();
System.out.println(comp_name.get());
}
}
इस वजह से इसमें get() फंक्शन मेथड का यूज़ केयरफुल्ली करना चाहिए।
orElse() function in the Optional Java class.
जावा प्रोग्राम में orElse() फंक्शन मेथड में ऑप्शनल एम्प्टी है, तो यह आउटपुट के रूप में एक डिफ़ॉल्ट वैल्यू को डिस्प्ले करता है।
Example of orElse() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.empty();
String output = comp_name.orElse(“Random”);
System.out.println(output);
}
}
its output.
Random
यदि इसमें कोई वैल्यू मौजूद है.
Optional<String> comp_name =
Optional.of(“Vcanhelpsu”);
String output =
comp_name.orElse(“Unknown”);
its output.
Vcanhelpsu
orElseGet() function in the Optional Java class.
जावा प्रोग्राम में orElseGet() फंक्शन मेथड एक सप्लायर वैल्यू लेता है, जो मौजूदा प्रोग्राम में ज़रूरत पड़ने पर एक डिफ़ॉल्ट वैल्यू को क्रिएट करता है।
Example of orElseGet() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.empty();
String output = comp_name.orElseGet(() -> “Unknown_value”);
System.out.println(output);
}
}
This is the main difference.
orElse(uservalue)
orElseGet(() -> uservalue)
यहाँ इस एक्साम्प्ल में orElse() फंक्शन अपने आर्गुमेंट को तब भी इवैल्यूएट करता है, जब ऑप्शनल में पहले से कोई वैल्यू होती है, जबकि orElseGet() सप्लायर इसे तभी इवैल्यूएट करता है, जब ऑप्शनल एम्प्टी होता है।
orElseThrow() function in the Optional Java class.
जावा प्रोग्राम में orElseThrow() फंक्शन में अगर कोई वैल्यू नहीं है, तो यहाँ एक्सेप्शन को थ्रो करने के लिए इसका यूज़ होता है
Example of orElseThrow() function.
In Java 8, the function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.empty();
String output = comp_name.orElseThrow(
() -> new IllegalArgumentException(“Company Name not found”)
);
System.out.println(output);
}
}
यदि यहाँ इसमें वैल्यू मौजूद है, तो यह एक नॉर्मली वैल्यू को रिटर्न होता है।
ifPresent() function in the Optional Java class.
जावा प्रोग्राम में ifPresent() फंक्शन मेथड में कोई एक्शन तभी एग्जीक्यूट करता है, जब कोई वैल्यू मौजूद हो।
Example of ifPresent() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.of(“Vcanhelpsu”);
comp_name.ifPresent(
n -> System.out.println(“comp_name = ” + n)
);
}
}
its output.
comp_name = Vcanhelpsu
यदि यहाँ ऑप्शनल एम्प्टी है, तो कुछ नहीं होता है।
filter() function in the Optional Java class.
जावा प्रोग्राम में फ़िल्टर() फंक्शन वैल्यू तभी रखता है, जब वह कोई कंडीशन को कम्पलीट करता हो।
Example of filter() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<Integer> integers = Optional.of(47);
Optional<Integer> output =
integers.filter(integer -> integer > 31);
System.out.println(output);
}
}
its output.
Optional[47]
यदि इसमें यूजर डिफाइन कंडीशन फाल्स है.
Optional<Integer> integers = Optional.of(7);
Optional<Integer> output =
integers.filter(integer -> integer > 31);
System.out.println(output);
its output.
Optional.empty
map() function in the Optional Java class.
जावा प्रोग्राम में map() फंक्शन मेथड एक ऑप्शनल के अंदर वैल्यू को रिप्लेस करता है।
Example of map() function.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> comp_name = Optional.of(“Vcanhelpsu”);
Optional<Integer> length =
comp_name.map(String::length);
System.out.println(length);
}
}
its output.
Optional[10]
Its flow is something like this.
“Vcanhelpsu”
↓
map(String::length)
↓
10
A complete example of Java Optional.
इसे इस प्रकार लिखने के बजाय।
public class Main {
public static void main(String[] args) {
String comp_name = getcomp_name();
if (comp_name != null) {
System.out.println(comp_name.toUpperCase());
} else {
System.out.println(“company name is not available”);
}
public static String getcomp_name() {
return “Vcanhelpsu”;
}
}
Java users can use an Optional.
Optional<String> comp_name =
Optional.ofNullable(getcomp_name());
String output =
comp_name.map(String::toUpperCase)
.orElse(“company name is not available”);
System.out.println(output);
यह एक्सप्रेशन मौजूदा जावा प्रोग्राम में किसी ऐसी वैल्यू की हैंडलिंग को क्लियर करता है, जो उसमे मौजूद न हो।
Important java Optional class Methods or function
| # | Optional Method | Purpose of optional method function |
| 1. | of() method | of() method function used tocreates optional with a non-null program value |
| 2. | ofNullable() method | ofNullable() method function used tocreate optional that may contain null info |
| 3. | empty() method | empty() method function used tocreate an empty optional value info |
| 4. | isPresent() method | isPresent() method function used tocheck whether value exists or not |
| 5. | get() method | get()method function used togets the value define by user |
| 6. | ifPresent() method | ifPresent() method function performs action if value exists |
| 7. | orElse() method | orElse() method function provides a default value |
| 8. | orElseGet() method | orElseGet() method function lazily provides a default value |
| 9. | orElseThrow() method | orElseThrow() method function used to throw an exception if empty |
| 10. | filter() method | filter() method function used tofilters the optional values |
| 11. | map() method | map() method function used totransforms the optional value |
Essential Java Optional Methods.
सिंपल फ़्लो
ऑप्शनल
│
┌───────┴───────┐
│ │
खाली प्रेजेंट
│ │
get()/map() orElse()
ifPresent() orElseGet()
orElseThrow()
Conclusion of Optional in Java 8.
जावा प्रोग्रामिंग लैंग्वेज में ऑप्शनल एक स्टोरेज कंटेनर है, जिसमें कोई यूजर डिफाइन वैल्यू हो सकती है, या ऑप्शनल अपने आप में एम्प्टी हो सकता है। ऑप्शनल क्लास मुख्य रूप से किसी वैल्यू की गैर-मौजूदगी को क्लियर करने और असुरक्षित नल हैंडलिंग एक्सेप्शन को कम करने के लिए यूज़फुल है।

