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

#Java
#API
📘 Java Generics and Collections راهنمای اصلی برای مفاهیم generics در جاوا بیش از یک دهه است. نسخه دوم این کتاب که بهروزرسانیهایی برای جاوا 21 دارد، یک راهنمای واضح و جامع برای generics از کاربردهای رایج تا پیچیدهترین حالات ارائه میدهد و همهچیز را برای استفاده و نوشتن APIهای generic بهطور مؤثر توضیح میدهد. این کتاب بهطور کامل کتابخانه collections را پوشش میدهد تا همیشه بدانید که کدام collection را برای هر کار خاص استفاده کنید. همچنین پردازش جریانها را توضیح میدهد تا بدانید کدام مدل را باید استفاده کنید و چگونه این مدلها برای بهدست آوردن بهترین نتیجه از کتابخانه پلتفرم با هم تعامل دارند.
🚀 این راهنمای ضروری شامل موارد زیر است:
🔄 نسخه دوم این کتاب یک بهروزرسانی جامع برای جاوا 21 است. با توجه به اینکه انگیزه اصلی برای نوشتن این نسخه از کتاب معرفی مجموعههای ترتیبی و تاثیر اولیه جریانها و لامبداها بود، پیشبینی میشد که تغییرات عمده نیاز به اصلاح در بخش دوم داشته باشد. اما این پیشبینی بهطور جدی کمارزیابی شد، چرا که زبان جاوا در 20 سال گذشته تغییرات زیادی کرده است. در حقیقت، بسیاری از تغییرات در بخش اول کتاب نیز لازم بود. زبان جاوا اکنون از ویژگیهای جدیدی مانند رکوردها، استنتاج نوع متغیرهای محلی، عبارات تطبیق الگو، و انواع generic در عبارات مقایسه نوع پشتیبانی میکند.
💡 یکی از انگیزههای مهم برای نوشتن نسخه دوم این بود که برخی از بینشهایی که جامعه جاوا در استفاده از generics و collections از سال 2005 بهدست آورده بود، ثبت شود. اینها عمدتاً در بخشی از کتاب به نام «بررسی طراحی generics جاوا» و دو فصل جدید در مورد collections آورده شدهاند: فصل 17 که راهنمای استفاده از چارچوب collections را ارائه میدهد و فصل 18 که مهمترین و جنجالیترین تصمیمات طراحی چارچوب collections را بررسی میکند.
👨💻 درباره نویسنده
موریس نافتالین مدیر فنی در شرکت Morningside Light Ltd. است، یک مشاوره نرمافزاری در بریتانیا.
فیلیپ وادلر استاد علوم کامپیوتر نظری در دانشگاه ادینبرو است که تحقیقاتی در زمینه طراحی زبانهای برنامهنویسی انجام میدهد. او یکی از طراحان GJ است که مبنای طراحی generics در نسخه 5.0 جاوا توسط Sun بود.
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.









