1. तपासत आहे

मला असे वाटते की डेटा प्रवाह एकत्र कसे करावे हे शिकून तुम्हाला आधीच कंटाळा आला असेल. तुम्हाला शेवटी डेटासह काहीतरी करायचे आहे.

वर्गामध्ये Streamतीन मानक पद्धती आहेत ज्या प्रवाह तयार करत नाहीत, परंतु त्याऐवजी त्यामध्ये कोणत्या प्रकारचा डेटा आहे ते तपासा. या पद्धती आहेत: anyMatch(), allMatch(), आणि noneMatch().

boolean anyMatch(rule)पद्धत

ही पद्धत स्ट्रीममध्ये किमान एक घटक आहे की नाही हे तपासते जे या पद्धतीला दिलेला नियम पूर्ण करतो. असा घटक असल्यास, पद्धत परत येते true, अन्यथा false.

उदाहरणे

कोड नोंद
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
boolean result = stream.anyMatch(x -> x > 0);

true
Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.anyMatch(x -> x > 0);

true
Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.filter(x -> x < 0).anyMatch(x -> x > 0);

false

शेवटच्या उदाहरणात, आम्ही प्रथम केवळ शून्यापेक्षा कमी घटक राखून ठेवतो आणि नंतर फिल्टर केलेले कोणतेही घटक शून्यापेक्षा मोठे आहेत की नाही हे पाहण्यासाठी आम्ही परिणाम तपासतो. अर्थात असे घटक आता राहिले नाहीत.

बुलियन ऑलमॅच(नियम) पद्धत

ही पद्धत प्रवाहातील सर्व घटक नियमाशी जुळतात की नाही हे तपासते (याला प्रिडिकेट असेही म्हणतात). नियम पद्धतीचा युक्तिवाद म्हणून पास केला जातो:

कोड नोंद
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
boolean result = stream.allMatch(x -> x > 0);
true
(शून्य पेक्षा मोठे सर्व घटक)
Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.allMatch(x -> x > 0);
false
(शून्य पेक्षा कमी किंवा समान घटक आहेत का?)
Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.filter(x -> x < 0).allMatch(x -> x < 0);
true
(शून्य पेक्षा कमी असलेले घटक आम्ही राखून ठेवले आहेत)

शेवटच्या उदाहरणात, आम्ही प्रथम केवळ शून्यापेक्षा कमी घटकांना फिल्टरमधून जाण्याची परवानगी देतो आणि नंतर आम्ही सर्व राखून ठेवलेले घटक शून्यापेक्षा कमी आहेत का ते तपासतो. चेकने सकारात्मक परिणाम दिला.

बुलियन noneMatch(नियम) पद्धत

noneMatch()स्ट्रीममध्ये पास केलेल्या नियमाशी जुळणारे कोणतेही घटक नाहीत की नाही हे पद्धत तपासते . हे पद्धतीच्या विरुद्ध आहे anyMatch().

कोड नोंद
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
boolean result = stream.noneMatch(x -> x > 0);

false
Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.noneMatch(x -> x > 0);

false
Stream<Integer> stream = Stream.of(1, -2, 3, -4, 5);
boolean result = stream.filter(x -> x < 0).noneMatch(x -> x > 0);

true

4
टास्क
Java Core,  पातळी 6धडा 5
लॉक केलेले
join: at the right time in the right place
The join method is very useful and is often used to terminate a thread. Think about where and on what object you need to call this method to have the result displayed
18
टास्क
Java Core,  पातळी 6धडा 5
लॉक केलेले
Horse racing
Programming is better than gambling... Still, horse races and other races perfectly illustrate multithreading! Figure out what the code does, and implement a method that counts the number of horses that have crossed the finish line. One more thing: you need to wait for the longshots to finish the race.

2. उपयुक्तता वर्ग: Optionalवर्ग

कधीकधी प्रोग्रामरसाठी nullसंदर्भांसह कार्य करणे खूप गैरसोयीचे असते. उदाहरणार्थ, समजा तुम्ही दोन तारांची तुलना करत आहात. दोन्ही व्हेरिएबल्स नसल्यास null, आपण फक्त कॉल करू शकता s1.equals(s2)आणि सर्वकाही कार्य करेल. परंतु जर s1असू शकत असेल null, तर तुम्हाला एक टाळण्यासाठी कोड लिहावा लागेल जो ही परिस्थिती हाताळेल NullPointerException.

म्हणूनच प्रोग्रामर Optional<T>युटिलिटी क्लाससह आले. त्याचा कोड अंदाजे असा दिसतो:

कोड नोंद
class Optional<Type>
{
   private final T type value;
   private Optional() { this.value = null;}
   private Optional(value) { this.value = value;}
   public static <Type> Optional<Type> of(Type value)
   {
      return new Optional<Type>(value);
   }

   public boolean isPresent()
   {
      return value != null;
   }

   public boolean isEmpty()
   {
      return value == null;
   }

   public Type get()
   {
      if (value == null)
      {
         throw new NoSuchElementException();
      }
      return value;
   }

   public Type orElse(Type other)
   {
      return value != null ? value : other;
   }

   public Type orElseThrow()
   {
      if (value == null)
      {
         throw new NoSuchElementException();
      }
      return value;
   }
}










मूल्य नाही आहे की नाही ते तपासते null



मूल्य आहे की नाही ते null




संचयित मूल्य परत करते. मूल्य शून्य असल्यास अपवाद टाकतो.







संचयित नॉन-नल मूल्य मिळवते. किंवा जर संचयित मूल्य आहे null, तर मेथड वितर्क म्हणून पास केलेले मूल्य परत करते



संचयित नॉन-नल मूल्य परत करते किंवा मूल्य शून्य असल्यास अपवाद फेकते.

या वर्गाचा उद्देश फक्त टी ऑब्जेक्ट संग्रहित करणे आहे (ज्या वस्तूचा प्रकार T आहे त्याचा संदर्भ). ऑब्जेक्टमधील ऑब्जेक्टचा संदर्भ Optional<T>असू शकतो null.

हा वर्ग प्रोग्रामरना थोडा सुंदर कोड लिहू देतो. चला तुलना करूया:

पर्यायी वापरणे पर्यायी वापरत नाही
public void printString(String s)
{
   Optional<String> str = Optional.ofNullable(s);
   System.out.println(str.orElse(""));
}
public void printString(String s)
{
   String str = s != null ? s : "";
   System.out.println(str)
}

एका ऑब्जेक्टची नेहमी पद्धत वापरून Optionalदुसर्‍या ऑब्जेक्टशी तुलना केली जाऊ शकते , जरी ते संदर्भ संग्रहित करतात.Optionalequalsnull

सोप्या भाषेत सांगायचे तर, Optionalवर्ग तुम्हाला "सुंदर" चेक लिहू देतो nullआणि एखाद्या Optionalवस्तूने nullमूल्य संग्रहित केल्यावर "सुंदर" क्रिया लिहू देतो.


9
टास्क
Java Core,  पातळी 6धडा 5
लॉक केलेले
Promotion during political debates
Mr Chump is our candidate! And we'll help him a bit by making him give a speech, and then another, and then another during a political debate. We'll help him a little by making him talk until all the available time has been taken. Threads come to our rescue!

3. घटक शोधणे

चला वर्गात परत जाऊया Stream. वर्गात Streamआणखी 4 पद्धती आहेत ज्या तुम्हाला प्रवाहातील घटक शोधू देतात. या पद्धती आहेत findFirst(), findAny(), min(), आणि max().

Optional<T> findFirst()पद्धत

पद्धत findFirst()फक्त प्रवाहातील पहिला घटक परत करते. एवढेच करतो.

येथे लक्षात घेण्यासारखी सर्वात मनोरंजक गोष्ट अशी आहे की पद्धत ऑब्जेक्ट परत करत नाही T, तर Optional<T>रॅपर ऑब्जेक्ट देते. nullहे सुनिश्चित करते की ऑब्जेक्ट शोधण्यात अयशस्वी झाल्यानंतर पद्धत कधीही परत येणार नाही .

उदाहरण:

ArrayList<String> list = new ArrayList<String>();
Collections.addAll(list, "Hello", "how's", "life?");
String str = list.stream().findFirst().get(); // Hello

अधिक स्पष्टीकरणासाठी, शेवटची ओळ अनेक ओळींमध्ये खंडित करूया:

ArrayList<String> list = new ArrayList<String>();
Collections.addAll(list, "Hello", "how's", "life?");

Stream<String> stream = list.stream();
Optional<String> result = stream.findFirst();
String str = result.get(); // Hello

शेवटची get()पद्धत म्हणजे ऑब्जेक्टमध्ये संग्रहित मूल्य पुनर्प्राप्त करणे Optional.

Optional<T> findAny()पद्धत

पद्धत findAny()प्रवाहातील कोणताही घटक परत करते आणि तिथेच संपते. ही पद्धत सारखीच आहे findFirst(), परंतु समांतर ऑपरेशन्समध्ये वापरल्या जाणार्‍या प्रवाहांसाठी ती उत्तम आहे.

समांतर प्रवाहांवर प्रक्रिया करताना, प्रवाहाच्या काही भागात तो घटक आधीच सापडलेला असू शकतो, परंतु तो पहिला आहे की नाही हे अद्याप स्पष्ट झालेले नाही.

जर अनेक घटक सर्व फिल्टर्सशी जुळले असतील आणि प्रोग्रामरला त्यापैकी पहिले अचूक मिळणे महत्त्वाचे असेल, तर ती findFirst()पद्धत म्हणायला हवी. जर प्रोग्रामरला माहित असेल की प्रत्यक्षात 0 किंवा 1 घटक सर्व फिल्टरशी जुळतील, तर फक्त कॉल करणे पुरेसे आहे findAny()- आणि हे जलद होईल.

Optional<T> min(Comparator<T>)पद्धत

पद्धत प्रवाहातील सर्व घटकांची तुलना करण्यासाठी ऑब्जेक्ट min()वापरते आणि किमान घटक परत करते. comparatorतुलनात्मक ऑब्जेक्ट परिभाषित करण्याचा सर्वात सोयीस्कर मार्ग म्हणजे लॅम्बडा फंक्शन.

सर्वात लहान स्ट्रिंग शोधण्याचे उदाहरण:

ArrayList<String> list = new ArrayList<String>();
Collections.addAll(list, "Hello", "how's", "life?");
String min = list.stream().min( (s1, s2)-> s1.length()-s2.length() ).get();

Optional<T> max(Comparator<T>)पद्धत

पद्धत प्रवाहातील सर्व घटकांची तुलना करण्यासाठी ऑब्जेक्ट max()वापरते आणि कमाल घटक परत करते. comparatorतुलनात्मक ऑब्जेक्ट परिभाषित करण्याचा सर्वात सोयीस्कर मार्ग म्हणजे लॅम्बडा फंक्शन.

सर्वात लांब स्ट्रिंग शोधण्याचे उदाहरण:

ArrayList<String> list = new ArrayList<String>();
Collections.addAll(list, "Hello", "how's", "life?");
String max = list.stream().max( (s1, s2)-> s1.length()-s2.length() ).get();
4
टास्क
Java Core,  पातळी 6धडा 5
लॉक केलेले
Justice
Justice is the name of an ancient Terrian deity that no one ever saw in the flesh. That said, Justice is sometimes encountered in man-made systems. We've got some mice here. For some reason, one of the mice, the alpha male, eats first while the others wait. Figure out why and remove this method call.
9
टास्क
Java Core,  पातळी 6धडा 5
लॉक केलेले
Arranging calls to join()
First, the cat gives birth to kittens. Then all the kittens climb out of the basket in random order. Finally, the cat brings them back into the basket. These events for one cat may be interspersed with events for another cat. Now implement this scenario with join().