public class Box<T> {
    private T content;
    
    public void setContent(T content) {
        this.content = content;
    }
    
    public T getContent() {
        return content;
    }
}Java Generic Type Naming convention helps us understanding code easily and having a naming convention is one of the best practices of Java programming language. So generics also comes with its own naming conventions. Usually, type parameter names are single, uppercase letters to make it easily distinguishable from java variables. The most commonly used type parameter names are:

E – Element (used extensively by the Java Collections Framework, for example ArrayList, Set etc.)
K – Key (Used in Map)
N – Number
T – Type
V – Value (Used in Map)
S,U,V etc. – 2nd, 3rd, 4th types// Generic method example 1
<T> void fromArrayToCollection(T[] a, Collection<T> c){
    for (T o: a){
        c.add(o);
    }
}

// Generic method example 2
public static <T extends SomeClass & SomeInterface> methodName(T o){
    o.setterMethod(123);
}

// Generic method example 3
public static <T> Stack <T> loadFromArray(Object[] arr, Class<T> type){
    Stack <T> stack = new StackArray<>(arr.length);
    for (Object o:arr){
        if (type.isInstance(o)){
            stack.push( (T) o); // type checking with "isInstance" and casting. "instanceof" will not work here.
        }
    }
}
<Car> c = loadFromArray(object_arr_of_cars, Car.class);
// generic methods

public <T> List<T> fromArrayToList(T[] a) {   
	    return Arrays.stream(a).collect(Collectors.toList());
	}

public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
	    return Arrays.stream(a)
	      .map(mapperFunction)
	      .collect(Collectors.toList());
	}

// bounded generics

public <T extends Number> List<T> fromArrayToList(T[] a) {
	    ...
	}

//multiple bounds

<T extends Number & Comparable>

// upper bound wildcards

public static void paintAllBuildings(List<? extends Building> buildings) {
	    ...
	}
    
// lower bound wildcard

<? super T>public class Tuple <T> {
  // the T is a placeholder for any datatype
  public T leftValue;
  public T rightValue;
  
  public Tuple(T leftValue, T rightValue){
    // again, T is being used as a placeholder for any type
    this.leftValue = leftValue;
    this.rightValue = rightValue;
}

public class Program{
  public static void main (String args){
    // And upon using Tuples we can fill in the T from the Tuple class with actual datatypes
    Tuple <int> intTuple = new Tuple <int>(5, 500)
    Tuple <String> stringTuple = new Tuple <String> ("Hello", "World")

    // we can even put Tuples inside of Tuples!
    Tuple<Tuple<int>> metaIntTuple = new Tuple <Tuple <int>> (intTuple, new Tuple <int> (456, 0));
  }
}public <T> List<T> fromArrayToList(T[] a) {   
    return Arrays.stream(a).collect(Collectors.toList());
}public class GenericFunction {
    public static void main(String[] args) {
        Integer[] intArray  = {1,2,3,4,5,6,7};
        Double[] doubleArray = {1.1,2.2,3.3,4.4,5.0};
        Character[] charArray = {'a' , 'b' , 'c' , 'd' , 'e'};
        System.out.println("Integer Array :");
        genericFunction(intArray);
        System.out.println();
        System.out.println("Double Array : ");
        genericFunction(doubleArray);
        System.out.println();
        System.out.println("Character Array : ");
        genericFunction(charArray);

    }
	// Generic Function In Java
    public static <T> void genericFunction(T[] nums){
        for(T element : nums){
            System.out.print(element + " ");
        }
    }
}
// Generic interface
interface BoxInterface<T> {
    void setValue(T value);
    T getValue();
}

// Generic class implementing the BoxInterface with T
class Box<T> implements BoxInterface<T> {
    private T value;

    @Override
    public void setValue(T value) {
        this.value = value;
    }

    @Override
    public T getValue() {
        return value;
    }
}

public class Main {
    public static void main(String[] args) {
        // Using T 
        Box<String> stringBox = new Box<>();
        stringBox.setValue("Hello, Generics!");
        String stringValue = stringBox.getValue();

        // Using ? (wildcard) with Interface
        BoxInterface<?> wildcardBox = stringBox;
        // wildcardBox.setValue("This won't compile"); // We cannot set value of unknown type
        Object wildcardValue = wildcardBox.getValue(); // We can only cast value to Object type

        // Using ? extends T (upper bounded wildcard) with Interface
        // Focus type : the type on the right side of extend
        BoxInterface<? extends String> upperBoundedWildcardBox = stringBox;
        // upperBoundedWildcardBox.setValue("This won't compile"); // We cannot set value of unknown type
        String upperBoundedWildcardValue = upperBoundedWildcardBox.getValue(); // We can only cast value to Parent type, String in this case

        // Using ? super T (loower bounded wildcard) with Interface
        // Focus type : the type on the left side of super
        BoxInterface<? super String> lowerBoundedWildcardBox = new Box<>();
        lowerBoundedWildcardBox.setValue("Lower Bounded Wildcard");  // We can set value to parent type, String in this case
        Object lowerBoundedWildcardValue = lowerBoundedWildcardBox.getValue(); // We can only cast value to Parent type, Object in this case
    }
}

Java相关代码片段

how to read all line from file java

Java int length

intent example in android

how to doubling size of an arrays in java

how to find my jdk path

how to handle multiple throws exception in java

spring boot jdbc for postgrest

factorial of a number

types of typecasting in java

types of assignment statement in java

Thread mutex

env files in spring boot

java over loading

logging in spring boot

how to split a list in multiple lists java

can two servlet have same urlpattern

spring debug

jsp spring

hanoi tower recursion java

java equals

age difference java

sed cheat sheet

java compare method

how to make a activity default in manifest

java max memory size

yum uninstall java

timestamp to long java

how to set background image in android studio

simple calculator in android

When to use HashMap vs Map

spring boot jpa

spring data jpa

spring jdbc

jpa vs hibernate

java with checking the password matching

profile in spring boot

string templates java

spring rest controller

java arraylist get item

com.oracle.jdbc

check if map is not empty java

how to install java 8 on debian 12

regex caractères spéciaux java

java 11 yum install

manage session in redis spring boot

java: error: release version 19 not supported

bean scope in spring

xml configuration in spring

spring cdi annotations

postconstruct and predestroy in spring

java decode_message

spring lazy initialization

java decorator design pattern

dependency injection in spring

check back pressed in fragment andorid

send email to any domain using java

record java

vs code setup for input/output in java

substring java

receive second word in string java

loose coupling in java

default value of char in java

spring boot repository pattern

spring boot h2 database

add security to spring admin

java islessthan method

Java implementation of recursive Binary Search

java round double

java downcasting

range of fibonacci series in java using recursion

java by anyone

solid java

equals() and hashcode() methods in java

android java map to bundle

android start activity with data

medium java 17

java import util list

bean life cycle in spring

spring boot aop

spring aspect