Use when Java generics including type parameters, wildcards, and type bounds. Use when writing type-safe reusable code.
View on GitHubTheBushidoCollective/han
jutsu-java
January 24, 2026
Select agents to install to:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-java/skills/java-generics/SKILL.md -a claude-code --skill java-genericsInstallation paths:
.claude/skills/java-generics/# Java Generics
Master Java's generics system for writing type-safe, reusable code with
compile-time type checking, generic classes, methods, wildcards, and
type bounds.
## Introduction to Generics
Generics enable types to be parameters when defining classes, interfaces,
and methods, providing compile-time type safety.
**Basic generic class:**
```java
public class Box<T> {
private T content;
public void set(T content) {
this.content = content;
}
public T get() {
return content;
}
public static void main(String[] args) {
// Type-safe box for String
Box<String> stringBox = new Box<>();
stringBox.set("Hello");
String value = stringBox.get(); // No casting needed
// Type-safe box for Integer
Box<Integer> intBox = new Box<>();
intBox.set(42);
Integer number = intBox.get();
}
}
```
**Generic with multiple type parameters:**
```java
public class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
public static void main(String[] args) {
Pair<String, Integer> pair = new Pair<>("age", 30);
String key = pair.getKey();
Integer value = pair.getValue();
}
}
```
## Generic Methods
Generic methods can be defined independently of generic classes.
**Basic generic method:**
```java
public class GenericMethods {
// Generic method
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.println(element);
}
}
// Generic method with return type
public static <T> T getFirst(T[] array) {
if (array.length > 0) {
return array[0];
}
return null;
}
public static void main(String[] args) {
String[] strings = {"a", "b", "c"};
Integer[]