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

ในตัวอย่างสุดท้าย อันดับแรกเราจะคงไว้เฉพาะองค์ประกอบที่น้อยกว่าศูนย์ จากนั้นเราจะตรวจสอบผลลัพธ์เพื่อดูว่ามีองค์ประกอบที่กรองแล้วมีค่ามากกว่าศูนย์หรือไม่ แน่นอนว่าไม่มีองค์ประกอบดังกล่าวอีกต่อไป

วิธีการบูลีน allMatch (กฎ)

วิธีนี้ตรวจสอบว่าองค์ประกอบทั้งหมดในสตรีมตรงกับกฎหรือไม่ (หรือที่เรียกว่าเพรดิเคต) กฎจะถูกส่งผ่านเป็นอาร์กิวเมนต์ไปยังเมธอด:

รหัส บันทึก
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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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จะส่งกลับค่าที่ส่งผ่านเป็นอาร์กิวเมนต์เมธอด



ส่งคืนค่าที่ไม่ใช่ค่า 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สามารถเปรียบเทียบได้เสมอกับOptionalวัตถุอื่นโดยใช้equalsเมธอด แม้ว่าจะเก็บnullข้อมูลอ้างอิงไว้ ก็ตาม

พูดง่ายๆOptionalคลาสนี้ให้คุณเขียนการตรวจสอบ "สวยงาม" nullและการกระทำ "สวยงาม" ในกรณีที่Optionalออบเจกต์เก็บnullค่าไว้


9
งาน
Java Core,  ระดับบทเรียน
ล็อค
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>ออบเจกต์ของ wrapper สิ่งนี้ทำให้มั่นใจได้ว่าเมธอดจะไม่กลับมา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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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().