An warware: java rafi nemo takamaiman kashi

rafi nemo takamaiman kashi 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 lambobi = Arrays.asList (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);'
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'abu mai dauke da lamba ta farko idan akwai.
5. A ฦ™arshe, muna amfani da idan Present() hanyar a kan `Optional`don buga ฦ™imar idan akwai.

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.

Shafi posts:

Leave a Comment