A cikin duniyar shirye-shirye, musamman a Java, akwai yanayi da yawa inda mutum zai buฦaci bincika takamaiman abu a cikin rafi. A cikin wannan labarin, za mu tattauna yadda ake nemo takamaiman abu a cikin rafi ta amfani da Java. Za mu bi ku ta hanyar gaba ษaya - daga samar da mafita ga matsalar, da kuma bayyana lambar ta hanyar mataki-mataki-mataki, tare da tattauna wasu ษakunan karatu da ayyuka masu dangantaka da zasu iya taimakawa yayin aiki akan matsalolin irin wannan.
Gabatarwa
Rafuffuka a cikin Java wani fasalin shirye-shirye ne mai ฦarfi da bayyanawa wanda aka gabatar a cikin Java 8. Suna ba da taฦaitacciyar hanya don bayyana hadaddun sarrafa bayanai da sauye-sauye akan tarin abubuwa kamar jeri da saiti. ฦayan aiki gama gari lokacin da ake mu'amala da tarin bayanai shine nemo takamaiman abin da ya dace da wasu sharuษษa ko sharuษษa. A cikin wannan labarin, za mu dubi yadda ake cim ma wannan ta amfani da rafukan Java.
import java.util.Arrays; import java.util.List; import java.util.Optional; public class StreamFindElement { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Find the first even number Optional<Integer> firstEven = numbers.stream() .filter(x -> x % 2 == 0) .findFirst(); firstEven.ifPresent(value -> System.out.println("First even number: " + value)); } }
Nemo wani Abu a cikin Rafi
- Rafin Java yana ba da hanyoyi akan tarin don aiwatar da ayyuka kamar tacewa, taswira da tattara sakamako bisa wasu sharuษษa.
- The tace ana amfani da hanyar don amfani da yanayi ga duk abubuwan da ke cikin rafi da kuma tace waษanda suka dace da yanayin.
- nemo Farko ana amfani da hanyar don nemo kashi na farko a cikin rafi wanda yayi daidai da yanayin da aka bayar.
Yanzu, bari mu rushe lambar mataki-mataki.
1. Da farko, mun ฦirฦiri jerin lambobi: `Jeri
2. Na gaba, muna amfani da rafi() hanya akan lissafin don ฦirฦirar rafi na lamba.
3. Don nemo lambar ko da ta farko, muna amfani da tacewa akan rafi ta amfani da hanyar `filter(x -> x % 2 == 0)`.
4. Bayan yin amfani da tacewa, muna kira da FindFirst() hanyar samun `Optional
5. A ฦarshe, muna amfani da idan Present() hanyar a kan `Optional
Java Zabi
- ZABI wani akwati ne wanda zai iya riฦe ฦima ko zama fanko. Ana amfani da shi don guje wa ma'amala tare da ฦimar banza da NullPointerExceptions a cikin lambar ku.
- The idan Present() Hanyar tana ษaukar alamar lambda ko hanyar da za a aiwatar kawai idan Zaษin ya ฦunshi ฦima.
- Zaษin yana da amfani musamman lokacin aiki tare da rafuffuka saboda suna wakiltar duka kasancewa da rashin ฦimar darajar da aka samo a cikin rafi bayan sarrafawa.
A ฦarshe, mun sami nasarar koyon yadda ake nemo takamaiman abu a cikin rafin Java ta amfani da tacewa da nemo hanyoyin farko. Har ila yau, mun yi la'akari da yin amfani da kayan aiki ZABI aji don gujewa ma'amala da ฦimar banza da NullPointerExceptions. Tabbatar da ci gaba da bincika ฦarin ayyuka da ษakunan karatu a Java don haษaka ฦwarewar ku da zama ingantaccen mai tsara shirye-shiryen rafi.