Skip to content

Commit 9ac0989

Browse files
Copilotbrunoborges
andauthored
Add raw-collections-to-generics pattern with translations and proof
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent dd591fc commit 9ac0989

19 files changed

Lines changed: 343 additions & 2 deletions

content/language/diamond-operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ support:
4343
state: "available"
4444
description: "Diamond with anonymous classes since JDK 9 (Sept 2017)."
4545
prev: "language/flexible-constructor-bodies"
46-
next: "language/private-interface-methods"
46+
next: "language/raw-collections-to-generics"
4747
related:
4848
- "language/type-inference-with-var"
4949
- "language/text-blocks-for-multiline-strings"

content/language/private-interface-methods.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ whyModernWins:
4949
support:
5050
state: "available"
5151
description: "Widely available since JDK 9 (Sept 2017)"
52-
prev: "language/diamond-operator"
52+
prev: "language/raw-collections-to-generics"
5353
next: "language/pattern-matching-switch"
5454
related:
5555
- "language/compact-source-files"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
id: 81fd0ae3-6d56-412a-a085-90d87c84f6fe
3+
slug: "raw-collections-to-generics"
4+
title: "Raw collections to generic types"
5+
category: "language"
6+
difficulty: "beginner"
7+
jdkVersion: "5"
8+
oldLabel: "Java 1.4"
9+
modernLabel: "Java 5+"
10+
oldApproach: "Raw collections"
11+
modernApproach: "Generic collections"
12+
oldCode: |-
13+
List names = new ArrayList();
14+
names.add("Duke");
15+
16+
String name = (String) names.get(0);
17+
modernCode: |-
18+
List<String> names = new ArrayList<>();
19+
names.add("Duke");
20+
21+
String name = names.getFirst();
22+
summary: "Replace raw collection types and retrieval casts with compile-time generic type safety."
23+
explanation: "Raw collections accept arbitrary object types and defer type errors until a cast executes.\
24+
\ Generic type arguments let the compiler validate writes and reads, remove casts, improve IDE assistance,\
25+
\ and make API contracts explicit. Raw types should normally remain only at carefully isolated legacy\
26+
\ interoperability boundaries."
27+
whyModernWins:
28+
- icon: "🔒"
29+
title: "Compile-time safety"
30+
desc: "Invalid element types fail during compilation rather than at runtime."
31+
- icon: "🧹"
32+
title: "No retrieval casts"
33+
desc: "Values retain their declared element type."
34+
- icon: "📖"
35+
title: "Self-documenting APIs"
36+
desc: "Collection declarations state exactly what they contain."
37+
support:
38+
state: "available"
39+
description: "Generics have been available since JDK 5 (September 2004)."
40+
prev: "language/diamond-operator"
41+
next: "language/private-interface-methods"
42+
related:
43+
- "language/diamond-operator"
44+
- "language/type-inference-with-var"
45+
- "collections/immutable-list-creation"
46+
tags:
47+
- generics
48+
- collections
49+
docs:
50+
- title: "Generics (The Java Tutorials)"
51+
href: "https://docs.oracle.com/javase/tutorial/java/generics/index.html"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
//JAVA 25+
3+
import java.util.*;
4+
5+
/// Proof: raw-collections-to-generics
6+
/// Source: content/language/raw-collections-to-generics.yaml
7+
void main() {
8+
List<String> names = new ArrayList<>();
9+
names.add("Duke");
10+
11+
String name = names.getFirst();
12+
}

social/queue.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ tooling/stack-walker
126126
collections/map-compute-and-merge
127127
io/http-websocket-client
128128
security/security-manager-migration
129+
language/raw-collections-to-generics

social/tweets.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,14 @@ security/security-manager-migration: |-
12771277
12781278
🔗 https://javaevolved.github.io/security/security-manager-migration.html
12791279
1280+
#Java #JavaEvolved
1281+
language/raw-collections-to-generics: |-
1282+
☕ Raw collections to generic types
1283+
1284+
Replace raw collection types and retrieval casts with compile-time generic type safety.
1285+
1286+
Raw collections → Generic collections (JDK 5+)
1287+
1288+
🔗 https://javaevolved.github.io/language/raw-collections-to-generics.html
1289+
12801290
#Java #JavaEvolved
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: من المجموعات الخام إلى الأنواع العامة
3+
oldApproach: مجموعات خام
4+
modernApproach: مجموعات عامة (Generics)
5+
summary: استبدل أنواع المجموعات الخام وعمليات التحويل عند الاسترجاع بأمان الأنواع في وقت الترجمة.
6+
explanation: "تقبل المجموعات الخام أي نوع كائن وتؤجل أخطاء الأنواع حتى تنفيذ عملية التحويل. تسمح وسائط الأنواع العامة\
7+
\ للمترجم بالتحقق من عمليات الكتابة والقراءة، وإزالة عمليات التحويل، وتحسين مساعدة بيئة التطوير، وجعل عقود واجهة\
8+
\ البرمجة صريحة. يجب أن تبقى الأنواع الخام عادةً فقط عند حدود التوافق القديمة المعزولة بعناية."
9+
whyModernWins:
10+
- icon: 🔒
11+
title: أمان وقت الترجمة
12+
desc: تفشل أنواع العناصر غير الصالحة أثناء الترجمة وليس وقت التشغيل.
13+
- icon: 🧹
14+
title: لا حاجة لتحويلات الاسترجاع
15+
desc: تحتفظ القيم بنوع العنصر المُعلن.
16+
- icon: 📖
17+
title: واجهات برمجية موثّقة ذاتيًا
18+
desc: تُبيّن تصريحات المجموعات بالضبط ما تحتويه.
19+
support:
20+
description: أصبحت الأنواع العامة متاحة منذ JDK 5 (سبتمبر 2004).
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: র (Raw) কালেকশন থেকে জেনেরিক টাইপে
3+
oldApproach: র কালেকশন
4+
modernApproach: জেনেরিক কালেকশন
5+
summary: র কালেকশন টাইপ ও রিট্রিভাল কাস্টের বদলে কম্পাইল-টাইম জেনেরিক টাইপ সেফটি ব্যবহার করুন।
6+
explanation: "র কালেকশন যেকোনো অবজেক্ট টাইপ গ্রহণ করে এবং টাইপ ত্রুটি কাস্ট কার্যকর না হওয়া পর্যন্ত পিছিয়ে দেয়। জেনেরিক\
7+
\ টাইপ আর্গুমেন্ট কম্পাইলারকে লেখা ও পড়া যাচাই করতে দেয়, কাস্ট সরিয়ে দেয়, IDE সহায়তা উন্নত করে এবং API চুক্তি স্পষ্ট করে\
8+
\ তোলে। র টাইপ সাধারণত শুধু সাবধানে পৃথক করা লিগ্যাসি ইন্টারঅপারেবিলিটি সীমানায় থাকা উচিত।"
9+
whyModernWins:
10+
- icon: 🔒
11+
title: কম্পাইল-টাইম নিরাপত্তা
12+
desc: অবৈধ এলিমেন্ট টাইপ রানটাইমের বদলে কম্পাইলেশনের সময় ব্যর্থ হয়।
13+
- icon: 🧹
14+
title: রিট্রিভাল কাস্ট নেই
15+
desc: মানগুলো তাদের ঘোষিত এলিমেন্ট টাইপ বজায় রাখে।
16+
- icon: 📖
17+
title: স্ব-ব্যাখ্যামূলক API
18+
desc: কালেকশন ঘোষণা ঠিক কী ধারণ করে তা স্পষ্ট করে বলে দেয়।
19+
support:
20+
description: জেনেরিকস JDK 5 (সেপ্টেম্বর ২০০৪) থেকে উপলব্ধ।
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Rohe Collections zu generischen Typen
3+
oldApproach: Rohe Collections
4+
modernApproach: Generische Collections
5+
summary: Ersetzen Sie rohe Collection-Typen und Downcasts durch generische Typsicherheit zur Kompilierzeit.
6+
explanation: "Rohe Collections akzeptieren beliebige Objekttypen und verschieben Typfehler bis zur Ausführung eines Casts.\
7+
\ Generische Typargumente lassen den Compiler Schreib- und Lesezugriffe prüfen, entfernen Casts, verbessern die\
8+
\ IDE-Unterstützung und machen API-Verträge explizit. Rohe Typen sollten normalerweise nur an sorgfältig isolierten\
9+
\ Altsystem-Schnittstellen verbleiben."
10+
whyModernWins:
11+
- icon: 🔒
12+
title: Sicherheit zur Kompilierzeit
13+
desc: Ungültige Elementtypen schlagen bereits beim Kompilieren fehl, nicht erst zur Laufzeit.
14+
- icon: 🧹
15+
title: Keine Downcasts
16+
desc: Werte behalten ihren deklarierten Elementtyp.
17+
- icon: 📖
18+
title: Selbsterklärende APIs
19+
desc: Collection-Deklarationen geben genau an, was sie enthalten.
20+
support:
21+
description: Generics sind seit JDK 5 (September 2004) verfügbar.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: De colecciones sin tipo a tipos genéricos
3+
oldApproach: Colecciones sin tipo
4+
modernApproach: Colecciones genéricas
5+
summary: Sustituye las colecciones sin tipo y los casts de recuperación por seguridad de tipos en tiempo de compilación.
6+
explanation: "Las colecciones sin tipo aceptan cualquier tipo de objeto y posponen los errores de tipo hasta que se\
7+
\ ejecuta un cast. Los argumentos de tipo genérico permiten que el compilador valide escrituras y lecturas, eliminen\
8+
\ los casts, mejoren la asistencia del IDE y hagan explícitos los contratos de la API. Los tipos sin tipar deberían\
9+
\ quedar reservados a puntos de interoperabilidad heredados cuidadosamente aislados."
10+
whyModernWins:
11+
- icon: 🔒
12+
title: Seguridad en tiempo de compilación
13+
desc: Los tipos de elemento inválidos fallan durante la compilación, no en tiempo de ejecución.
14+
- icon: 🧹
15+
title: Sin casts de recuperación
16+
desc: Los valores conservan su tipo de elemento declarado.
17+
- icon: 📖
18+
title: APIs autodocumentadas
19+
desc: Las declaraciones de colecciones indican exactamente qué contienen.
20+
support:
21+
description: Los genéricos están disponibles desde JDK 5 (septiembre de 2004).

0 commit comments

Comments
 (0)