Explain Wildcards generic in java

Explain Wildcards generic in java

Wildcards in Java generics provide a way to specify a range of allowable types or unknown types when working with generic types. They offer flexibility in defining methods or classes that can accept a variety of generic types or unknown types without explicitly specifying them.

Wildcards generic

1. Upper Bounded Wildcards (? extends T):

This wildcard allows any type that is a subtype of the specified type T or T itself. It is used when you want to work with a collection of objects of a specific type or its subclasses.

2. Lower Bounded Wildcards (? super T):

This wildcard allows any type that is a supertype of the specified type T or T itself. It is used when you want to add elements to a collection but do not care about their exact types as long as they are of a superclass of T.

3. Unbounded Wildcards (?):

This wildcard represents an unknown type. It is useful when you want to work with a collection without caring about the type of its elements.

Example
Java Example:
java
import java.util.ArrayList;
import java.util.List;

public class WildcardsExample {
    // Upper bounded wildcard method
    public static double sumOfList(List<? extends Number> list) {
        double sum = 0.0;
        for (Number num : list) {
            sum += num.doubleValue();
        }
        return sum;
    }

    // Lower bounded wildcard method
    public static void addIntegers(List<? super Integer> list) {
        for (int i = 1; i <= 5; i++) {
            list.add(i);
        }
    }

    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        List<Double> doubleList = new ArrayList<>();

        // Add elements to the lists
        integerList.add(1);
        integerList.add(2);
        doubleList.add(3.0);
        doubleList.add(4.0);

        // Call the upper bounded wildcard method
        double sum1 = sumOfList(integerList); // Allowed, as List<Integer> is a subtype of List<? extends Number>
        double sum2 = sumOfList(doubleList);  // Allowed, as List<Double> is a subtype of List<? extends Number>

        System.out.println("Sum of integers: " + sum1); // Output: Sum of integers: 3.0
        System.out.println("Sum of doubles: " + sum2);  // Output: Sum of doubles: 7.0

        // Call the lower bounded wildcard method
        List<Number> numberList = new ArrayList<>();
        addIntegers(numberList); // Allowed, as List<Number> is a supertype of List<? super Integer>
        System.out.println("Number list after adding integers: " + numberList); // Output: Number list after adding integers: [1, 2, 3, 4, 5]
    }
}

In this example, sumOfList() is a method that accepts a list of numbers or its subtypes using an upper bounded wildcard (? extends Number). It calculates the sum of the elements in the list. addIntegers() is a method that adds integers to a list of numbers or its supertypes using a lower bounded wildcard (? super Integer). It adds integers to the list. These methods demonstrate how wildcards can be used to work with collections of unknown or specific types in a flexible manner.

Homepage

Readmore