Fundamentals and Recommended Practices
Maurice Naftalin and Philip Wadler with Stuart Marks

#Java
#API
🚀 کتاب Java Generics and Collections برای بیش از یک دهه، مرجع اصلی و بدون رقیب برای یادگیری جنریکها بوده است. این ویرایش دوم، نسخه Java 21 رو به طور کامل پوشش میده و یک راهنمای واضح برای درک جنریکها از رایجترین کاربردها گرفته تا عجیبترین و خاصترین سناریوها (Corner cases) ارائه میکنه؛ خلاصه هر چیزی که برای نوشتن و استفاده بهینه از APIهای جنریک نیاز دارید رو در اختیارتون میگذاره. این کتاب کتابخانه کالکشنها رو به طور جامع بررسی میکنه تا همیشه بدونید برای یک تسک مشخص، چطور و چه زمانی از کدوم کالکشن استفاده کنید. همچنین پردازش استریمها (Stream processing) رو کاملاً تشریح میکنه تا مدل مناسب رو بشناسید و متوجه بشید چطور این ابزارها با هم کار میکنند تا بهترین خروجی رو از پلتفرم بگیرید.
💡 مبانی جنریکها: پارامترهای تایپ (Type parameters) و متدهای جنریک
🔄 زیرنوعها (Subtyping) و وایلدکاردها (Wildcards)
🔍 جنریکها و رفلکشن (Reflection)
🧱 پترنهای دیزاین برای جنریکها
📊 مجموعهها، صفها، لیستها، مپها و پیادهسازی آنها
🔐 برنامهنویسی همزمان (Concurrent programming) و ایمنی ترد (Thread safety) در کالکشنها
⚡ بررسی پرفورمنس پیادهسازیهای مختلف کالکشن
🚀 بهترین روشها (Best practices) برای استفاده و توسعه فریمورک کالکشنهای جاوا
🧠 فلسفه دیزاین و مقایسه با سایر کتابخانههای کالکشن
بخش اول: جنریکها
۱. مقدمه
۲. زیرنوعها و وایلدکاردها
۳. مقایسه و باندها (Bounds)
۴. تعاریف (Declarations)
۵. تایپهای Reifiable و Nonreifiable
۶. رفلکشن
۷. جنریکهای کارآمد (Effective Generics)
بخش دوم: کالکشنها
۸. اینترفیسهای اصلی فریمورک کالکشن جاوا
۹. مقدمات
۱۰. اینترفیس Collection
۱۱. اینترفیس SequencedCollection
۱۲. مجموعهها (Sets)
۱۳. صفها (Queues)
۱۴. لیستها (Lists)
۱۵. مپها (Maps)
۱۶. کلاس Collections
۱۷. راهنمای استفاده از فریمورک کالکشن جاوا
۱۸. مروری بر دیزاین و گذشته
💡 ویرایش دوم این کتاب یک آپدیت جامع برای Java 21 هست. از اونجایی که انگیزه اصلی نوشتن این ویرایش، معرفی SequencedCollection و تاثیرات قبلی استریمها و لمبداها بود، قبل از شروع کار انتظار داشتم که تغییرات اصلی فقط توی بخش دوم (کالکشنها) باشه. اما این یک دستکمگرفتن جدی برای تغییراتی بود که جاوا تو ۲۰ سال گذشته به خودش دیده؛ در واقع بازبینیهای زیادی روی بخش اول هم نیاز شد. زبان جاوا تکامل پیدا کرده؛ حالا از رکوردها (Records)، استنتاج تایپ متغیرهای محلی (Local variable type inference)، اکسپرشنهای Pattern-matching و تایپهای جنریک توی اکسپرشنهای مقایسه تایپ پشتیبانی میکنه. همچنین بیشتر مثالهای بخش اول آپدیت شدن تا تغییرات کتابخانههای پلتفرم رو منعکس کنن؛ مواردی مثل Deprecate شدن کانستراکتورهای Integer، متدهای استاتیک روی اینترفیس Comparator و معرفی استریمها و کالکشنهای غیرقابلتغییر (Unmodifiable collections).
📈 یک انگیزه مهم دیگه برای ویرایش دوم، ثبت تجربیات و بینشهایی بود که کامیونیتی جاوا در استفاده از جنریکها و کالکشنها از سال ۲۰۰۵ به دست آورده. این نکات بیشتر توی بخش مرور گذشته ("درباره دیزاین جنریکهای جاوا") و دو چپتر جدید کالکشن قرار گرفتن: چپتر ۱۷ که راهنمای استفاده از فریمورک هست و چپتر ۱۸ که حیاتیترین و البته بحثبرانگیزترین تصمیمات پشت دیزاین این فریمورک رو بررسی میکنه. علاوه بر این، چپترهای Reification (چپتر ۵) و جنریکهای کارآمد (چپتر ۷) به شکل اساسی بازنویسی شدن، چپتر پترنهای دیزاین حذف شده و چپتر مربوط به مهاجرت از کدهای قدیمی (قبل از ظهور جنریک) به عنوان ضمیمه برای علاقهمندان به مباحث فنی و تاریخی به صورت آنلاین در دسترس قرار گرفته.
🎯 این کتاب برای هر کسی نوشته شده که شناختی از جاوا داره و میخواد دانش خودش رو عمیقتر کنه. این اثر یک توتوریال مقدماتی جاوا نیست؛ بنابراین ما فرض رو بر این گذاشتیم که شما مفاهیم پایه جاوا مثل کلاسها، متدهای استاتیک و Instance نویسی رو کاملاً بلدید. ما از سینتکس Java 21 استفاده میکنیم، اما بیشتر ویژگیهای سینتکس که مشخصاً به جنریکها مربوط میشن رو توی متن توضیح دادیم. اگه موقع درک سینتکس یک نمونه کد به چالش خوردید، پیشنهادات ریفکتورینگ IDE شما میتونه هدف اون کد رو کاملاً براتون روشن کنه.
💻 همچنین فرض ما اینه که یک آشنایی خیلی اولیه با Collections API دارید. اگه تا حالا برنامهای نوشتید که توش از ArrayList یا HashSet استفاده شده، هیچ مشکلی برای همراهی با کتاب نخواهید داشت. موقع بررسی رفتار متدها و کلاسهای کالکشن خاص، ما اغلب به داکیومنتهای رسمی جاوا ارجاع میدیم؛ چرا که در صورت وجود هرگونه ابهام، داکیومنت رسمی جاوا مرجع نهایی و دقیق هست.
👨💻 موریس نافتالی مدیر فنی شرکت Morningside Light Ltd هست که یک شرکت مشاوره نرمافزاری در بریتانیاست.
👨💻 فیلیپ وادلر استاد علوم کامپیوتر تئوری در دانشگاه ادینبرو هست، جایی که تحقیقاتش روی دیزاین زبانهای برنامهنویسی تمرکز داره. اون یکی از طراحان اصلی پروژه GJ بوده؛ همان پروژهای که بعداً به پایه و اساس اضافه شدن جنریکها در Sun's Java 5.0 تبدیل شد.
Java Generics and Collections has been the go-to guide to generics for more than a decade. This second edition covers Java 21, providing a clear guide to generics from their most common uses to the strangest corner cases, giving you everything you need to know to use and write generic APIs effectively. It covers the collections library thoroughly, so you'll always know how and when to use each collection for any given task. And it explains stream processing, so you'll know which model to use and how they interoperate to get the best out of the platform library.
This indispensable guide covers:
Table of Contents
Part I. Generics
Chapter 1. Introduction
Chapter 2. Subtyping and Wildcards
Chapter 3. Comparison and Bounds
Chapter 4. Declarations
Chapter 5. Reifiable and Nonreifiable Types
Chapter 6. Reflection
Chapter 7. Effective Generics
Part II. Collections
Chapter 8. The Main Interfaces of the Java Collections Framework
Chapter 9. Preliminaries
Chapter 10. The Collection Interface
Chapter 11. The SequencedCollection Interface
Chapter 12. Sets
Chapter 13. Queues
Chapter 14. Lists
Chapter 15. Maps
Chapter 16. The Collections Class
Chapter 17. Guidance for Using the Java Collections Framework
Chapter 18. Design Retrospective
The second edition of this book is a comprehensive update for Java 21. Since a major impetus for writing it was the introduction of sequenced collections and the earlier impact of streams and lambdas, I expected before starting work that the major changes needed would be in Part II, principally as a result of the introduction of sequenced collections. However, that was a serious underestimate of the changes that Java has undergone in the last 20 years: in fact, many revisions to Part I were also required. The language has changed; it now supports records, local variable type inference, pattern-matching expressions, and generic types in type comparison expressions. Also, most examples in Part I needed updating to take account of changes in the platform libraries, including the current deprecation of the Integer constructors, static methods on the Comparator interface, and the introduction of unmodifiable collections and of streams.
An important motivation for writing a second edition was to record some of the insights gained by the Java community in the use of both generics and collections since 2005. These are mainly contained in a retrospective section (“On the Design of Java Generics”) and two new collections chapters: Chapter 17, which provides guidance for use of the Collections Framework, and Chapter 18, which reviews the most crucial—and also the most controversial—decisions underlying the choices made in the design of the framework. In addition, the chapters on reification (Chapter 5) and effective generics (Chapter 7) have been substantially rewritten, the chapter on design patterns has been dropped, and the chapter on migration from pre-generic code has been made available online as an Appendix for technical and historical interest.
I have preserved the preface to the first edition, in an updated form, at the end of this section. I thought this was worthwhile to convey some of the sense of excitement that accompanied the finely coordinated collection of features with which Java 5 brought the Java language into the 21st century.
Maurice Naftalin
Edinburgh, February 2025
Intended Audience
This book is intended for everyone who knows something about Java and would like to find out more. It’s not a Java tutorial, so we assume knowledge of basic Java concepts like classes, static and instance methods, and so on. We use the syntax of Java 21, but most syntax features associated specifically with generics are explained in the text. If you are having difficulty understanding the syntax of a particular code sample, you may find that your integrated development environment (IDE) can offer helpful refactoring suggestions that will clarify its purpose.
We assume very basic knowledge of the Collections API. If you have written any programs using ArrayList or HashSet, you should have no difficulty. When discussing the behavior of particular collection classes and methods, we often refer to the Java documentation. When in doubt, the Java documentation provides the definitive specification.
About the Author
Maurice Naftalin is Technical Director at Morningside Light Ltd., a software consultancy in the United Kingdom.
Philip Wadler is Professor of Theoretical Computer Science at theUniversity of Edinburgh, where his research focuses on the design of programming languages. He is a co-designer of GJ, work that became the basis for generics in Sun's Java 5.0.









