About the grammar for defining Java generic classes and methods. See here for terms → Points of Java Generics
For example, if `` `List , then
Eenclosed in angle brackets is the pseudo parameter. In terms of grammar, there is no problem with two or more characters or mixed lowercase letters, but by convention, one character is often used.
Or
There are some restrictions because the type parameter information is cleared at run time.
-- new T () `` `cannot be done --
new T [] `` `cannot be done
-- T.class
cannot be referenced
--obj instance of T
cannot be evaluated
If you write `SomeClass <T> ```, you can use its type parameter
`T``` in the fields of the class and in the methods (including arguments and return values).
//↓ Write the type parameter here
public class SomeClass<T> {
private T t;
private List<T> list = new ArrayList<T>();
public void set(T arg){
this.t = arg;
}
public T get(){
return this.t;
}
}
Note that the formal parameters of a generic class cannot be used in static methods or static statements in that class as shown below.
public class Hoge<T>{
static {
T t; //Compile error
}
public static void hoge(T t){ //Compile error
}
}
If you write `<T>`
before the return type of a method, you can use its type parameter `` `T``` in the method.
//↓ Write the type parameter here
public static <T> ArrayList<T> someMethod(T arg){
ArrayList<T> list = new ArrayList<T>();
list.add(arg);
return list;
}
That's all for the basics of generic class definition and method definition, and then we will change the contents of the pseudo parameter `<T>`
depending on the case.
You can use multiple type parameters separated by commas.
public static <K, V> HashMap<K, V> newHashMap(){
return new HashMap<K, V>();
}
// ※<K,V>The type of is guessed from the left side
Map<String, Integer> map = newHashMap();
<t extends classes and interfaces>
If you write liket
Can require the caller to be a subclass of the specified class (or the specified class itself).
//ArrayList that can only be instantiated with a Subclass of Number type or Number type
public class NumArrayList<T extends Number> extends ArrayList<T> {}
List<Number> numberList = new NumArrayList<Number>(); // ok
List<Double> doubleList = new NumArrayList<Double>(); // ok
List<String> stringList = new NumArrayList<String>(); //Compile error
This example requires the caller that E is an implementation class of Comparable
//Method to retrieve maximum value from collection
public static <E extends Comparable<E>> E max(Collection<E> collection) {
//Code omitted. E is compareTo(E)Because it has a method
//You can use it to implement an algorithm to find the maximum value of an element.
}
To specify that it is an implementation class of multiple interfaces, connect the parent class and interfaces with `&`
.
//Requires E to implement both Comparable and Serializable interfaces
public class Fuga<E extends Comparable<E> & Serializable>{}
("& ``` Is a very unfamiliar symbol, but I'm using ``` & ``
because ```, `` `is already used as the type parameter delimiter. I think that the)
Recommended Posts