This page looks best with JavaScript enabled

Generics and Collections

 ·  ☕ 3 min read  ·  ✍️ t1

Java Generic is a feature of the Java language that allows developers to define classes, interfaces, and methods that can operate on different types of objects, without having to specify the specific types of objects that they will operate on. This allows developers to write code that is more flexible and reusable, and can be applied to a wider range of situations.

Java Collections is a framework that provides a number of classes and interfaces for storing and manipulating collections of objects. These classes and interfaces are designed to be used with Java Generic, allowing developers to create collections that can store objects of any type.

Here is an example of using Java Generic and Collections to create a simple class that stores a list of numbers:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class NumberList<T extends Number> {
    private List<T> numbers;

    public NumberList() {
        this.numbers = new ArrayList<>();
    }

    public void addNumber(T number) {
        this.numbers.add(number);
    }

    public T getNumber(int index) {
        return this.numbers.get(index);
    }

    public int size() {
        return this.numbers.size();
    }
}

In this example, the NumberList class is defined using Java Generic, with a type parameter T that extends the Number class. This means that the NumberList class can store objects of any type that is a subtype of Number, such as Integer, Double, or BigDecimal.

The NumberList class has a private field numbers that stores a list of numbers using the ArrayList class from the Java Collections framework. This list is parameterized with the T type parameter, which means that it can store objects of any type that is a subtype of Number.

The NumberList class also has a number of methods for adding, retrieving, and querying the numbers in the list. These methods use the ArrayList methods to perform their operations, but they are parameterized with the T type parameter, which means that they can operate on objects of any type that is a subtype of Number.

To use the NumberList class, we can simply create an instance of the class and pass the desired type as the type parameter:

1
2
3
NumberList<Integer> integerList = new NumberList<>();
NumberList<Double> doubleList = new NumberList<>();
NumberList<BigDecimal> decimalList = new NumberList<>();

In this example, we create three instances of the NumberList class, each parameterized with a different type. The first instance is parameterized with the Integer type, which means that it can only store Integer objects. The second instance is parameterized with the Double type, which means that it can only store Double objects. And the third instance is parameterized with the BigDecimal type, which means that it can only store BigDecimal objects.

We can then use the addNumber method to add numbers to each of the instances of the NumberList class:

1
2
3
4
5
integerList.addNumber(1);
integerList.addNumber(2);
integerList.addNumber(3);

doubleList.addNumber(1.1);
Share on

t1
WRITTEN BY
t1
Dev