انتقل إلى المحتوى
أمين أبوزيد
BEG-036

Stream API

  • 8 درس
  • 88 دقيقة قراءة
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • filter
  • map
  • flatMap
  • reduce
  • collectors
  • التجميع (grouping)

تعلّم أولًا

88 دقيقة قراءة · 4 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Build pipelines with filter, map, flatMap, sorted, distinct, and limit.
  2. Terminate streams with collect, reduce, count, anyMatch, and findFirst.
  3. Group and aggregate with Collectors.groupingBy, counting, and summingDouble.
  4. Decide when a plain loop is clearer than a stream.

الدروس

  1. 01Streams مقابل الحلقات12 د قراءة
  2. 02العمليات الوسيطة10 د قراءة
  3. 03العمليات النهائية والتقييم الكسول (laziness)12 د قراءة
  4. 04flatMap10 د قراءة
  5. 05Collectors والتجميع11 د قراءة
  6. 06متى لا تستخدم stream11 د قراءة
  7. 07عرض عملي: بناء خط معالجة تقرير المبيعات10 د قراءة
  8. 08عرض عملي: إعادة كتابة stream غير مقروء على هيئة حلقة12 د قراءة

المختبر والتمارين

Lab

BEG-036-LAB1 · Sales report

Outcomes: 1–4 · Time: ~2.5 h From a list of orders compute revenue by category, top customers, and average order value per month. Keep each pipeline short and readable; note any step you wrote as a loop and why. Acceptance: correct results, readable pipelines.

Exercises (assessment: six exercises)

IDOutcomeTask
BEG-036-EX011From a list of names, produce the distinct, upper-case names longer than 3 letters, sorted, first 5 only.
BEG-036-EX022Given integers, compute the sum with reduce, test whether any is negative, and count the evens.
BEG-036-EX031Given List<Order> where each has List<String> items, produce a flat list of distinct item names.
BEG-036-EX043Group people by city and count them; then group orders by category and sum the amounts.
BEG-036-EX052Find the first person older than 60 and print their name, or "none".
BEG-036-EX064Review: rewrite a 10-step stream chain with a side effect as a loop, and justify the change in two sentences.
أظهر الحلول
  • EX01 names.stream().filter(n -> n.length() > 3).map(String::toUpperCase).distinct().sorted().limit(5).toList();
  • EX02 reduce(0, Integer::sum); anyMatch(n -> n < 0); filter(n -> n % 2 == 0).count().
  • EX03 orders.stream().flatMap(o -> o.getItems().stream()).distinct().toList();
  • EX04 groupingBy(Person::getCity, counting()); groupingBy(Order::getCategory, summingDouble(Order::getAmount)).
  • EX05 people.stream().filter(p -> p.getAge() > 60).findFirst() returns an Optional; for now use .map(Person::getName).orElse("none").
  • EX06 A named-variable loop with the side effect inside; the answer says the loop is easier to read and debug and the side effect is explicit.
  • Reusing a consumed stream is covered in demo L07 and quiz Q09.

الاختبار

Outcomes: O1 intermediate operations · O2 terminal operations · O3 grouping and collectors · O4 streams vs loops.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Which keeps only matching items?O1
  2. 2Which transforms each item into another value?O1
  3. 3flatMap is used to:O1
  4. 4A pipeline with no terminal operation:O2
  5. 5findFirst() returns:O2
  6. 6Which combines all items into one value?O2
  7. 7Collectors.groupingBy(Person::getCity) returns:O3
  8. 8To count items per group, use as downstream:O3
  9. 9Using a stream a second time after a terminal operation:O1
  10. 10A loop is usually clearer when:O4

الجزء العملي

Six exercises BEG-036-EX01 to EX06 plus the lab. Pass: at least five exercises correct, EX06 justified, and all lab results correct.

أخطاء شائعة

Side effects inside map; reusing a consumed stream; unreadable 15-step chains.

التقييم

6 exercises.

الصلة بالمسار الوظيفي

Everyday Java; analogous to SQL GROUP BY (BEG-050).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.