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
(பூஜ்ஜியத்தை விட குறைவான உறுப்புகளை நாங்கள் தக்கவைத்துள்ளோம்)

கடைசி எடுத்துக்காட்டில், முதலில் பூஜ்ஜியத்திற்குக் குறைவான உறுப்புகளை மட்டுமே வடிகட்டி வழியாகச் செல்ல அனுமதிக்கிறோம், பின்னர் தக்கவைக்கப்பட்ட அனைத்து உறுப்புகளும் பூஜ்ஜியத்தை விடக் குறைவாக உள்ளதா என்பதைச் சரிபார்க்கிறோம். காசோலை நேர்மறையான முடிவை அளிக்கிறது.

boolean 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 பொருளை சேமிப்பதாகும் (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"அழகான" காசோலைகள் மற்றும் "அழகான" செயல்களை எழுத வகுப்பு உங்களை அனுமதிக்கிறது .nullOptionalnull


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().