Home  >  Article  >  Java  >  What should you pay attention to when using java generic methods?

What should you pay attention to when using java generic methods?

PHPz
PHPzforward
2023-05-18 08:16:05899browse

1. Notes

Generic methods can exist in either generic classes or ordinary classes. If using generic methods can solve the problem, then you should try to use generic methods.

The type parameter T in the generic method and the type parameter in the generic class are of different types.

2. Example

//注意这个T是一种全新的类型,可以与泛型类中声明的T不是同一种类型。
public <T> void PrinterInfo(T e) {
    System.out.println(e);
}
//调用方法
DataHolder<String> dataHolder=new DataHolder<>();
dataHolder.PrinterInfo(1);
dataHolder.PrinterInfo("AAAAA");
dataHolder.PrinterInfo(8.88f);

What collection classes are there in Java?

Collections in Java are mainly divided into four categories:

1 , List list: ordered, repeatable;

2, Queue queue: ordered, repeatable;

3, Set collection: non-repeatable;

4. Map mapping: unordered, unique keys, but not unique values.

The above is the detailed content of What should you pay attention to when using java generic methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete