Home> Java> javaTutorial> body text

Generic wildcards in Java functions: upper and lower bounds

WBOY
Release: 2024-04-25 16:18:02
Original
580 people have browsed it

In Java, generic wildcards allow generic types to be represented without specifying specific types. The upper bound wildcard character () represents a type parameter T or any subclass thereof, allowing subclass data to be accepted in a list. The lower wildcard character () represents T or any of its superclasses, allowing the superclass data in the list to be accepted. Wildcards can be used to implement resizable array lists with generics, allowing handling of various types and addition/removal operations.

Java 函数中的泛型通配符:上限和下限

Generic wildcards in Java functions: upper and lower bounds

In Java, we can use wildcards to represent generics Types allow us to use generics without specifying a specific type. This article will explore upper and lower wildcards and demonstrate their usage through practical examples.

Upper limit wildcard character

Upper limit wildcard character () represents the type parameterTor any of its subclasses kind. For example:

public static  double sum(List numbers) { double total = 0; for (T num : numbers) { total += num.doubleValue(); } return total; }
Copy after login

This function can accept any Number (such as Integer, Double) or a list of its subclasses. We can safely pass any list that satisfies the Number constraint to this function as follows:

List ints = List.of(1, 2, 3); double sumIntegers = sum(ints); // 编译通过
Copy after login

Lower bound wildcard

Lower bound wildcard() represents the type parameterTor any of its superclasses. For example:

public static  void process(List entities) { for (T entity : entities) { System.out.println(entity.getClass().getName()); } }
Copy after login

This function can accept a list of any Number superclass (such as Object, Serializable). We can safely pass any list that satisfies the Number superclass constraints to this function as follows:

List objects = List.of("Hello", 123); process(objects); // 编译通过
        
Copy after login

Practical Case

Consider a resizable array List, we can use generic wildcards to achieve it:

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ResizableArrayList { private List list; public ResizableArrayList() { this(new ArrayList<>()); } public ResizableArrayList(List initialList) { list = new ArrayList<>(initialList); } public void addAll(List elements) { list.addAll(elements); } public void removeAll(List elements) { list.removeAll(elements); } public List getList() { return list; } public static void main(String[] args) { ResizableArrayList numbers = new ResizableArrayList<>( Arrays.asList(1, 2, 3) ); numbers.addAll(Arrays.asList(4, 5, 6)); numbers.removeAll(Arrays.asList(2, 3)); System.out.println(numbers.getList()); // [1, 4, 5, 6] } }
Copy after login

This resizable array list can handle any typeE, allowing us to add or remove various objects to the list.

The above is the detailed content of Generic wildcards in Java functions: upper and lower bounds. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!