Simple answer
Yes.
More detailed answer
You can use a generic collection without a value <T>, for example:
ArrayList a = new ArrayList();
a.add(2);
a.add("String");
Using collections without <T>is a bad habit, and most IDEs / compilers give a warning here. You can get around it using the collection Object, i.e.:
ArrayList<Object> a = new ArrayList<Object>();
, , ArrayList<Number>, Number, BigDecimal, BigInteger, Byte, Double, Float, , , :
ArrayList<Number> a = new ArrayList<Number>();
a.add(2);
a.add(42L);
a.add(123.45d);
System.out.println(a.toString());
, , a Number - .. - (, # isInfinite(), Number ), typecast , - typecast:
a.get(2).isInfinite()
((Double) a.get(2)).isInfinite()
((Double) a.get(1)).isInfinite()
, , , .
, ArrayList<Integer> , ( ) ArrayList<Number>, .. :
public void printNumbers(ArrayList<Number> list) {
list.forEach(System.out::println);
}
ArrayList<Integer> a = new ArrayList<Integer>();
printNumbers(a);
:
public void printIntegers(ArrayList<Integer> list) {
list.forEach(System.out::println);
}
ArrayList<Number> a = new ArrayList<Number>();
printIntegers(a);
, ArrayList<Number>, , ArrayList<? extends Number> ArrayList<? super Number>. extends , (.. ) , super , (.. ). , , extends:
public void printNumbers(ArrayList<? extends Number> list) {
list.forEach(System.out::println);
}
ArrayList<Integer> listInt = new ArrayList<Integer>();
printNumbers(listInt);
ArrayList<Double> listDbl = new ArrayList<Double>();
printNumbers(listDbl);
<? T > < T > Java .