So in line 16 I have the method declaration for newArrayList. For it to be accepted, it needs < T > before and after ArrayList.
Please let me know if I have understood this correctly. The second < T > is the return type of the ArrayList. So this method will return an ArrayList that contains objects of type T.
However, I don't know the purpose of the first < T > from the method declaration. What does that do?
package com.codegym.task.task35.task3509;
import java.util.*;
/*
Collections & Generics
*/
public class Solution {
public static void main(String[] args) {
}
public static <T> ArrayList <T> newArrayList(T... elements) {
//write your code here
ArrayList<T> arrayList = new ArrayList<>();
for (Object obj : elements){
arrayList.add((T) obj);
}
return arrayList;
}
public static HashSet newHashSet(Object... elements) {
//write your code here
return null;
}
public static HashMap newHashMap(List keys, List values) {
//write your code here
return null;
}
}