search
HomeJavajavaTutorialHow to use Java generic methods

How to use Java generic methods

May 01, 2023 pm 02:16 PM
java

Generic methods

are generally defined as follows, that is, a

public class FTest {
	public <t> List<t> f(T t){...};
}</t></t>

is added in front of the method. Three generic parameter inference methods:

1. Directly add the determined generic type

fTest.<integer>f(xxx)</integer>

2 in front of f(), and determine it through the input parameters. The following inference is Integer

int number = 0;
fTest.f(number)

3, which can be passed Return value OK

List<integer> list = fTest.f(xxx);</integer>

Q: What is wrong with the following code? Is toString() there?

public class A<t> {
	public static void  test(T t){
  		System.out.println(t.toString());  
  }
}</t>

A: test is a static method, so the T
in the A instance cannot be sensed and needs to be changed to
public static void test(T t)

toString() There is no problem, toString is the method of Object.

Generic parameters and type elimination

Q: What will the generic parameter T become at runtime?
A: Unified into Object and does not contain any type information.

Q: Can instanceof be used for comparison of generic parameter T?

class A<t> {
   void f(Object arg)
   if(arg instanceof T) {
	  ...
   }
}</t>

A: No, the compiler will report an error.

Q: Can the new T() or new T[] operation be performed on the generic parameter T?
A: No, the compiler will report an error.

How to use Java generic methods

#Q: Can I call methods in generic parameter objects?

T.f();

A: Only methods of Object can be called.

Q: Can T be used for forced conversion?

T t = (T)object;

A: It can run, but the transformation will not actually occur, and a warning warning will be triggered during compilation.

Questions when creating a new generic object

Assume that there are two classes, the base class Parent and the subclass Child

class Parent{}
class Child extends Parent{}

Answer the following questions:
Q: Below Is there something wrong with this sentence?

List<parent> list = new ArrayList<child>()</child></parent>

A: There is a problem, the compilation is wrong. There is no parent-child relationship between List and ArrayList

Q:

List extends Parent> list = new ArrayList<child>();</child>

What are the characteristics of this list?

A: This list can call A a = list.get(), but cannot list.add(new Parent())

  • Reason:
    list What .get() does is to force-convert the internal extend Parent> to Parent when returning. This is reasonable. Any subclass of Parent can be converted to Parent
    list.add(new Parent ()) is to convert the external A into the internal extend Parent> during input. This is unreasonable because we don't know which Parent subclass this Parent object can be converted into.

Q:

List super Child> list = new ArrayList<parent>();</parent>

What are the characteristics of this list?
Who will report an error below

list.add(new Child())
list.add(new Parent())
Parent a= list.get();
Child b = list.get()

A: The screenshot is as follows:

How to use Java generic methods

  • ##Child c = list.get() or Parent The operation done by p = list.get() is to force the internal super Child> to the external Parent or child when returning, which is unreasonable because the compiler thinks that the child's parent class

    Not necessarily can be converted to parent or child, so this behavior is prohibited (for example, the parent class of parent is object, but object may not be converted to parent or child). The operation done by *list.add(new Child()) is to convert the external child or parent into the internal super Child> during input. This is reasonable, because child and parent can definitely be converted into child 's parent class.

Q:

List> list = new ArrayList<a>();</a>
What are the characteristics of this list?

A: Neither get nor add can be used, only operations such as remove with no return value and no input A can be performed.

PS: Note that it does not mean that the get or add method cannot be called, but that when calling get or add, the object A cannot be used to operate.
That is, you cannot do add(A) or A a = get(0)
But you can do add(object) or Object o = get(0)
Because? It can be converted to Object, but cannot be converted to A.

Q: Will the following code report an error?

   List<fruit> fruitList = new ArrayList();
   fruitList.add(new Fruit());
   List<apple> appleList = new ArrayList();
   appleList.add(new Apple());
   fruitList.addAll(appleList);
   System.out.println(fruitList);</apple></fruit>
A: No error will be reported. The results will be printed normally.

How to use Java generic methods

PECS principle

Pay attention to the difference between the PECS principle and the above!
The ? extend or ? supert mentioned above are both used when declaring objects.
The PECS principle is used for method input parameters of generic objects!

Suppose there is a class defined as follows:

public static class MyList<t> {
    List<t> list = new ArrayList();

    // 把输入参数塞给自己,类似于生产操作
    public void pushList(List<t> t) {
        list.addAll(t);
    }

    // 把自己的内容塞给输入参数,类似于让输入参数做消费。
    public void pollList(List<t> t) {
         t.addAll(list);
    }
}</t></t></t></t>
Then T is the generic parameter.

Q: Can the following code run normally?

MyList<number> myList = new MyList();

List<integer> intList = new ArrayList();
myList.pushList(intList);

List<object> objectList = new ArrayList();
myList.pollList(objectList);</object></integer></number>
A: It cannot run normally, both pushList and pollList will report errors

Because after checking, the compiler thinks that List and List are not the same thing!

Q: If we want to support pushList above, how should we modify the definition of pushList method?

A: Change it to this:

// 把输入参数塞给自己,类似于生产操作
public void pushList(List extends T> t) {
    list.addAll(t);
}
That is, the compiler thinks that List and List extend Number> are the same thing, allowed!

Q: If you want to support pollList , how to modify the definition?

A:

// 把自己的内容塞给输入参数,类似于让输入参数做消费。
public void pollList(List super T> t) {
    t.addAll(list);
}

因为是把自己的东西塞给输入参数, 而想要能塞进去,必须保证自己这个T,是输入参数的子类,反过来说,输入参数必须是T的父类,所以用super
于是编译器认为,List 和List super Number>是一个东西,允许!

PECS原则出自Effective Java, 注意只是一个编程建议而已!

  • 如果有一个类A,泛型参数为T

  • 如果他一般只用于接收输入容器List后,塞入自己内部的T容器, 则类A就叫生产者, 因此输入参数最好定义为 extend T>最好, 以便能接收任何T子类的容器。

  • 如果他一般只用于接收输入容器后List, 把自己内部的T元素塞给它, 那么这个类A就叫消费者, 输入参数最好定义为 super T>\ 最好, 以便自己的T元素能塞给任何T元素的父类容器。

The above is the detailed content of How to use Java generic methods. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools