When programming, I want to implement classes and methods with multiple types. It's easy to see, for example, that it's ridiculous to implement an ArrayList for an int, a double for a String, respectively. ArrrayList \ <T > is an example of Generic Programming, as it can handle lists of various type parameters in the same way. Very useful. Creating a class or method with multiple type parameters surrounded by \ <> like ArrayList \ <T > is called Generic Programming.
This article first explains the implementation and usage of the basic GenericClass and GenericMethod. Then, I will introduce how to limit GenericPrograming and how to handle it flexibly. Finally, I will summarize the points to note about GenericProgramming in Java.
Generic Class A simple implementation of GenericClass.
Entry.java
public class Entry<K, V>{ // point 1
private K key; // point 2
private V value;
public Entry(K key, V value){ // point 2
this.key=key;
this.value=value;
}
public K key(){return key;} // point 2
public V value(){return value;}
}
Client.java
Entry<Integer, String> id = new Entry<String, Integer>(1001, "hitoshi watanabe"); //1
Entry<Integr, String> id2 = new Entry<>(1002, "taro yamada"); //2
var id3 = new Entry<Integer, String>(1003, "hideo kojima"); //3
Primitive types cannot be used for type parameters, so boxed Integers are used instead of ints.
Generic Method Type parameters are instance-dependent, so if you want to use type parameters in a static method, you need to declare type parameters that are independent of the instance.
Arrays.java
public class Arrays {
public static <T> void swap(T[] array, int i, int j){ // point 1
T tmp = array[i]; //point 2
array[i]=array[j];
array[j]=tmp;
}
}
Client.java
String[] member = ...;
Arrays.<String>swap(member, 0, 1); //1
Arrays.swap(member, 0, 1); //2
A sufficiently abstract operation like Array.swap could declare a type parameter called \ <T > which does not limit the type. But when you want to do a more restrictive operation, the type parameter is satisfied. You need to set a limit so that only subtypes of the type are included. As an example, we will introduce the implementation when you want to close all the elements by taking ArrayList of the class that implements the AutoClosable interface as an argument.
Sample1.java
public static <T extends AutoClosable> void closeAll(ArrayList<T> elems) throws Exception{ // point 1
for(T e : elems) e.close(); // point 2
}
When \ <T > is declared, the compiler complements \ <T extends Object >. In java, all classes have an Object class in their ancestors, so the methods provided by the Object class can be used.
closeAll method does not have to be GenericMethod
Sample.java
public static void closeAll(AutoClosable[] elems){
for(AutoClosable e : elems) e.close();
}
##Increased flexibility in Generic Programming
When you want to implement a method that takes an instance with a type parameter as an argument,Instance subtype to take as an argument,It's normal to want to allow supertypes as arguments as well..However,
#### **`BadSample.java`**
```java
public static void printName(ArrayList<Employee> staff){
for(Employee e : staff) System.out.println(e.getName());
}
BadSample.ArrayList as an argument like java
###SubType WildCard BadSample.In java, I couldn't take a class that inherited Employee as an argument.,SubType Wild Card is used to solve the problem..
Sample2.java
public static void printName(ArrayList<? extends Emplyee> staff){ // point 1
for(Employee e : staff) System.out.println(e.getName());
}
<? extends hoge>Will accept an instance of hoge's subtype parameter as an argument.hoge subtypes can be treated as hoge in polymorphism.At that time,If the method used is overridden,Overridden method is called.
Recommended Posts