मुझे लगता है कि optionals अब जावा मानक लाइब्रेरी में हैं। लेकिन एक मूल समस्या है जिसमें मैं चल रहा हूं जिसमें मुझे पता नहीं लगाया गया है कि कैसे सर्वोत्तम (आसानी से पढ़ने और समझने, सबसे सुंदर, सबसे छोटा) तरीका हल करने के लिए:वैकल्पिक विकल्प खाली होने पर वापस कैसे करें?
वैकल्पिक होने पर विधि से कैसे वापस आना है खाली है?
मैं एक सामान्य समाधान की तलाश में हूं जो वैकल्पिक विकल्पों और कोड ब्लॉक के आकार के विभिन्न संयोजनों के लिए काम करता है। वैकल्पिक और संक्षिप्तता के सुरक्षित रूप से प्रकार:
void m1() {
// When I get an optional:
Optional<String> o = getOptional();
// And want to return if it's empty
if (!o.isPresent()) return;
// In the whole rest of the method I have to call Optional.get
// every time I want the value:
System.out.println(o.get());
// Which is pretty ugly and verbose!
}
void m2() {
// If I instead return null if a value is absent:
String s = getNullabe();
if (s == null) return;
// Then I can use the value directly:
System.out.println(s);
}
यह सवाल कैसे ऊपर दोनों उदाहरण के अच्छे पहलू पाने के लिए के बारे में है:
निम्न उदाहरण में मैं दिखाने के लिए मैं क्या मतलब है की कोशिश करेंगे निरर्थक प्रकार के।
बाकी के उदाहरण इस और अधिक दिखाते हैं।
void m3() {
// If I on the other hand want to throw on empty that's pretty and compact:
String s = getOptional()
.orElseThrow(IllegalStateException::new);
System.out.println(s);
}
void m4() {
Optional<String> o = getOptional();
if (!o.isPresent()) return;
// I can of course declare a new variable for the un-optionalised string:
String s = o.get();
System.out.println(s);
// But the old variable still remains in scope for the whole method
// which is ugly and annoying.
System.out.println(o.get());
}
void m5() {
// This is compact and maybe pretty in some ways:
getOptional().ifPresent(s -> {
System.out.println(s);
// But the extra level of nesting is annoying and it feels
// wrong to write all the code in a big lambda.
getOtherOptional().ifPresent(i -> {
// Also, more optional values makes it really weird and
// pretty hard to read, while with nullables I would
// get no extra nesting, it would looks good and be
// easy to read.
System.out.println("i: " + i);
// It doesn't work in all cases either way.
});
});
}
Optional<String> getOptional() {
throw new UnsupportedOperationException();
}
Optional<Integer> getOtherOptional() {
throw new UnsupportedOperationException();
}
String getNullabe() {
throw new UnsupportedOperationException();
}
कैसे मैं एक विधि से लौटा सकते हैं या एक वैकल्पिक खाली है विधि के बाकी हिस्सों में get
का उपयोग किए बिना, एक अतिरिक्त चर घोषित करने के बिना और ब्लॉक घोंसले के अतिरिक्त स्तरों के बिना?
या यदि यह सब प्राप्त करना संभव नहीं है, तो इस स्थिति को संभालने का सबसे अच्छा तरीका क्या है?
वास्तव में 'if (वैकल्पिक.isPresent()) {...} 'के साथ कुछ भी गलत नहीं है। 'वैकल्पिक '' शून्य' से अधिक अर्थपूर्ण मान जोड़ता है, इसलिए मिशन पूरा हुआ। पठनीयता और अल्पसंख्यक अक्सर एक अच्छा संतुलन होते हैं। "बदसूरत" और "verbose" यहां इस तरह के अतिव्यक्ति हैं। – Radiodef
@Lii यदि आप अधिक सामान्य मामले (वैकल्पिक प्रकार के विकल्प और संचालन) में रूचि रखते हैं, तो कृपया इसे अपने प्रश्न में जोड़ें। यदि आपके पास समान प्रकार और समान ऑपरेशन हैं तो यह एक अलग उपयोग केस है। – user140547
यह सभी उदाहरण जैसे उदाहरण वैकल्पिक विकल्प का उपयोग सबसे अच्छा समाधान नहीं है ... –