-1

What's the general rule about when a type parameter list is required?

For example, the type parameter list <E> seen between static and the return type Set<E> below: is it required?

public static <E> Set<E> union(Set<E> s1, Set<E> s2) {}

For another example, writing out <E> is not required in the push() or pop() methods below:

public class Stack<E> {
    public Stack();
    public void push(E e);
    public E pop();
}
SOLO
  • 768
  • 6
  • 19
Zoe
  • 807
  • 3
  • 10
  • 16

1 Answers1

0

maybe this example illustrates how to use the function properly:

public static void main(String[] args) {
    Set<Integer> s1 = new HashSet<>();
    Set<Integer> s2 = new HashSet<>();

    Set<Integer> result = union(s1, s2);
}

public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
    // do something useful here
}
oschlueter
  • 2,248
  • 1
  • 16
  • 38