Generics let you write a class or method once and have the compiler check the types it works with. When you write List<String>, the compiler rejects list.add(42) and lets you read elements without a cast. The angle-bracket names such as T, E, K and V are type parameters: placeholders that are replaced by real type arguments at each use. The diamond <> on the right side (new ArrayList<>()) asks the compiler to infer the type argument from the left side.
Keep reading for free
Create a free StudyToCert account to read the rest of this lesson: 5 more sections, 5 key terms, a real-world example, an exam tip and self-check questions. Every lesson, lab and practice test is free with an account.