Frequently used Java generics

I was confused about how to use generics (generic type), so I wrote an example that I often use.

I want to decide as an argument of the class

public class Foo<T> {
    T t;
    public T get() {
        return t;
    }
    public void set(T t) {
        this.t = t;
    }
}

When using multiple parameters, separate them with `Foo <T, U>` and commas.

I want to force a subclass

public class Foo<T extends Parent> {
    T t;
    public T get() {
        return t;
    }
    public void set(T t) {
        this.t = t;
    }
}

Only when using a method

It is inconvenient when you want to make a temporary decision if you specify it for the entire class. For example, when you want to convert a List to an array

List list = new ArrayList<String>();
list.add("foo1");
list.add("foo2");
list.add("foo3");
String[] strList = list.toArray(new String[list.size()]);

I think you may use the toArray method like this. The implementation at this time

public class Foo {
    public <T> T get(T t) {
        return t;
    }
}

At this time, T is determined by looking at the argument type.

Cannot create an instance

Not surprisingly, it cannot be `new T ()`. The reason is that there is no guarantee that an instance can be created unless you know who T is.

public class Foo<T extends Parent> {
    public T get() {
        return new T(); //Can not!!
    }
}

It seems that you can create an instance from the Class class using java refactoring to achieve coding like new T (), but the performance seems to drop significantly.

Recommended Posts

Frequently used Java generics
[Java] Generics
Java Generics Summary
[Java] Generics sample
Frequently used docker-compose command
Docker Frequently used commands
List of frequently used Java instructions (for beginners and beginners)
IntelliJ Frequently Used Operation Notes
[Java] Generics classes and generics methods
Frequently used processes in SpreadSheet
java Generics T and? Difference
rails console Frequently used operations
Frequently used Maven command collection
[Personal memo] Frequently used Java grammar updated from time to time
Syntax examples often used in Java
git flow Frequently used commands Memo
Frequently used methods in Active Record
Design patterns to enjoy with frequently used Java libraries --Factory pattern
Summary of frequently used Docker commands
Effective Java 3rd Edition Chapter 5 Generics
Java
[Java] Try to implement using generics
Java generics (defines classes and methods)
Generics of Kotlin for Java developers
[Docker] Other frequently used (probably) Docker Instructions
Java
Why Java Vector is not used
Introduction to Docker (1) Frequently used commands
[Personal notes] Frequently used IntelliJ IDEA shortcuts
Frequently used functions and commands of Xcode
[EMMET notation] Basic, frequently used guy Emmet notation summary
[Java] Introducing the Generics Boundary Wildcard Type
Java Summary of frequently searched type conversions
About the meaning of type variables, E, T, etc. used in generics used in Java