泛型是 Java 中一种强大的机制,它允许你编写可以与多种数据类型一起工作的代码,而无需在编译时指定具体的类型。这样可以提高代码的灵活性、可读性和安全性。
泛型可以帮助我们编写更安全、更灵活、更易于维护的代码。具体来说,它带来的优势有:
3.1 定义泛型类
// 定义一个泛型类,类型参数为 T
public class GenericClass<T> {
    private T data;
    // 构造函数
    public GenericClass(T data) {
        this.data = data;
    }
    // 获取数据的方法
    public T getData() {
        return data;
    }
}3.2 使用泛型类
// 使用 Integer 类型创建 GenericClass 实例
GenericClass<Integer> integerGeneric = new GenericClass<>(10);
// 使用 String 类型创建 GenericClass 实例
GenericClass<String> stringGeneric = new GenericClass<>("Hello");
// 获取数据
System.out.println("Integer data: " + integerGeneric.getData()); // 输出:Integer data: 10
System.out.println("String data: " + stringGeneric.getData()); // 输出:String data: Hello3.3 定义泛型方法
public class GenericMethods {
    // 定义一个泛型方法,类型参数为 T
    public static <T> void printArray(T[] array) {
        for (T element : array) {
            System.out.print(element + " ");
        }
        System.out.println();
    }
}
// 使用泛型方法
public static void main(String[] args) {
    Integer[] intArray = {1, 2, 3};
    String[] strArray = {"a", "b", "c"};
    // 使用泛型方法打印数组
    GenericMethods.printArray(intArray); // 输出:1 2 3
    GenericMethods.printArray(strArray); // 输出:a b c
}// 使用 ArrayList 存储字符串
List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Peter");
// 使用 HashMap 存储键值对
Map<String, Integer> ages = new HashMap<>();
ages.put("John", 30);
ages.put("Jane", 25);
ages.put("Peter", 28);public class SortUtils {
    public static <T extends Comparable<T>> void sort(T[] array) {
        // 使用冒泡排序算法对数组进行排序
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j].compareTo(array[j + 1]) > 0) {
                    T temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }
}public class GenericStack<T> {
    private T[] data;
    private int top;
    public GenericStack(int capacity) {
        this.data = (T[]) new Object[capacity];
        this.top = -1;
    }
    public void push(T element) {
        if (top == data.length - 1) {
            throw new StackOverflowError();
        }
        data[++top] = element;
    }
    public T pop() {
        if (top == -1) {
            throw new EmptyStackException();
        }
        return data[top--];
    }
    public T peek() {
        if (top == -1) {
            throw new EmptyStackException();
        }
        return data[top];
    }
    public boolean isEmpty() {
        return top == -1;
    }
}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, String> stringPair = new Pair<>("name", "John Doe");
        System.out.println("Key: " + stringPair.getKey() + ", Value: " + stringPair.getValue());
        // 创建一个整数键值对
        Pair<Integer, Integer> integerPair = new Pair<>(1, 2);
        System.out.println("Key: " + integerPair.getKey() + ", Value: " + integerPair.getValue());
    }
}import java.util.HashMap;
import java.util.Map;
public class GenericCache<K, V> {
    private Map<K, V> cache = new HashMap<>();
    public V get(K key) {
        return cache.get(key);
    }
    public void put(K key, V value) {
        cache.put(key, value);
    }
    public static void main(String[] args) {
        // 创建一个字符串缓存
        GenericCache<String, String> stringCache = new GenericCache<>();
        stringCache.put("name", "John Doe");
        System.out.println("Name: " + stringCache.get("name")); // 输出:Name: John Doe
        // 创建一个整数缓存
        GenericCache<Integer, Integer> integerCache = new GenericCache<>();
        integerCache.put(1, 2);
        System.out.println("Value: " + integerCache.get(1)); // 输出:Value: 2
    }
}这段代码是一个泛型方法,用于根据给定的 id 在 personList 中查找并返回对应的对象。
/**
 * 根据给定的 ID 查找 personList 中的对象。
 *
 * @param id 要查找的对象 ID
 * @return 找到的对象,如果未找到则返回 null
 * @throws IllegalAccessException 如果访问字段时出现非法访问异常
 */
public <T> T findById(int id) throws IllegalAccessException {
    // 遍历 personList 中的每一个元素
    for (int i = 0; i < personList.size(); i++) {
        // 将当前元素强制转换为泛型 T
        T person = (T) personList.get(i);
        // 获取当前对象的类类型
        Class clazz = person.getClass();
        // 获取该类的所有字段列表
        List<Field> allFieldList = getAllField(clazz);
        
        // 遍历所有字段
        for (Field field : allFieldList) {
            // 设定字段可访问
            field.setAccessible(true);
            // 检查字段的值是否等于传入的 ID
            if (field.get(personList.get(i)).equals(id)) {
                // 找到匹配的对象,返回它
                return person;
            }
        }
    }
    // 如果没有找到,返回 null
    return null;
}总结: 泛型是 Java 中一个强大的工具,它可以使你的代码更灵活、更安全、更易于阅读和维护。通过泛型,你可以编写一次代码,就可以适用于多种数据类型,从而减少代码冗余和错误。希望这篇文章能够帮助各位看官理解泛型的概念和应用,并将其应用到你的 Java 开发中。感谢各位看官的观看,下期见,谢谢~