Java generics: some general options?

I was wondering if it is possible to write a function that accepts several common types as follows:

public int void myfunction(Set<T> a, Set<T> b) { return 5; } Set<Integer> setA = new HashSet<Integer>(); Set<String> setB = new HashSet<String>(); int result = myfunction(setA, setB); 

Will this work? Does the general parameter in each parameter generalize that each parameter must have the same type T as the general?

Thank!

+41
java generics parameters
Nov 18 '09 at 22:17
source share
6 answers

Yes - it is possible (although not with your method signature) and yes, the types must be the same with your signature.

With the signature you specified, T must be associated with one type (e.g. String or Integer ) on the call site. You can, however, declare method signatures that accept multiple parameters of type

 public <S, T> void func(Set<S> s, Set<T> t) 

Note the above signature that I declared types S and T in the signature itself. Therefore, they are different and do not depend on any common types associated with a class or interface that contains this function.

 public class MyClass<S, T> { public void foo(Set<S> s, Set<T> t); //same type params as on class public <U, V> void bar(Set<U> s, Set<V> t); //type params independent of class } 

Perhaps you should take a look at some of the signature methods of collection classes in the java.util package. Generics are a pretty tricky question, especially when looking at wildcards ( ? extends and ? super ). For example, it often happens that a method that can take Set<Number> as a parameter must also accept Set<Integer> . In this case, you will see the following signature:

 public void baz(Set<? extends T> s); 

There are many questions that are already there for SO so you can look at this topic!

  • Java Generics: List, List <Object>, List <? >
  • Java Generics (Wildcards)
  • What are the differences between Generics in C # and Java ... and C ++ templates?

Not sure if the return point is int from the function, although you can do it if you want!

+78
Nov 18 '09 at 10:20
source share

You can declare several types of variables of type or method. For example, using type parameters for a method:

 <P, Q> int f(Set<P>, Set<Q>) { return 0; } 
+6
Nov 18 '09 at 22:21
source share

Moreover, you can inherit generics :)

 @SuppressWarnings("unchecked") public <T extends Something<E>, E extends Enum<E> & SomethingAware> T getSomething(Class<T> clazz) { return (T) somethingHolderMap.get(clazz); } 
+5
Aug 08 '13 at 9:13
source share

a and b must both be sets of the same type. But nothing is stopping you from writing

 myfunction(Set<X> a, Set<Y> b) 
+2
Nov 18 '09 at 22:19
source share

In defining a function, you hold back sets a and b for the same type. You can also write

 public <X,Y> void myFunction(Set<X> s1, Set<Y> s2){...} 
+2
Nov 18 '09 at 22:22
source share

You can perform one of the following approaches:

1) Basic, single type:

 //One type public static <T> void fill(List <T> list, T val) { for(int i=0; i<list.size(); i++){ list.set(i, val); } } 

2) Several types:

 // multiple types as parameters public static <T1, T2> String multipleTypeArgument(T1 val1, T2 val2) { return val1+" "+val2; } 

3) The compiler error will be raised below, since "T3 is not included in the list of generic types that are used in the function declaration part.

 //Raised compilation error public static <T1, T2> T3 returnTypeGeneric(T1 val1, T2 val2) { return 0; } 

Right: compiles fine

 public static <T1, T2, T3> T3 returnTypeGeneric(T1 val1, T2 val2) { return 0; } 

Class code example:

 package generics.basics; import java.util.ArrayList; import java.util.List; public class GenericMethods { /* Declare the generic type parameter T in this method. After the qualifiers public and static, you put <T> and then followed it by return type, method name, and its parameters. Observe : type of val is 'T' and not '<T>' * */ //One type public static <T> void fill(List <T> list, T val) { for(int i=0; i<list.size(); i++){ list.set(i, val); } } // multiple types as parameters public static <T1, T2> String multipleTypeArgument(T1 val1, T2 val2) { return val1+" "+val2; } /*// Q: To audience -> will this compile ? * * public static <T1, T2> T3 returnTypeGeneric(T1 val1, T2 val2) { return 0; }*/ public static <T1, T2, T3> T3 returnTypeGeneric(T1 val1, T2 val2) { return null; } public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(10); list.add(20); System.out.println(list.toString()); fill(list, 100); System.out.println(list.toString()); List<String> Strlist = new ArrayList<>(); Strlist.add("Chirag"); Strlist.add("Nayak"); System.out.println(Strlist.toString()); fill(Strlist, "GOOD BOY"); System.out.println(Strlist.toString()); System.out.println(multipleTypeArgument("Chirag", 100)); System.out.println(multipleTypeArgument(100,"Nayak")); } } 

// class definition is completed

Output Example:

 [10, 20] [100, 100] [Chirag, Nayak] [GOOD BOY, GOOD BOY] Chirag 100 100 Nayak 
0
Jul 04 '17 at 0:15
source share



All Articles