Java Generic Methods Examples

In this article, we will learn how to write a generic method with examples.

The Syntax for Defining Generic Method

<type-Parameters> return_type method_name(parameter list)
{
  // ..
}
The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method's return type. For static generic methods, the type parameter section must appear before the method's return type.

Java Generic Methods Examples

Generic Methods Example to Convert Array to ArrayList

In this example. we have used Java 8 features so JDK 8 or later is required to compile and execute this program.
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Generic methods example to convert array to list.
 * @author javaguides.net
 *
 */
public class GenericMethodsExamples {
    // definition of a generic method
    public static <T> List<T> fromArrayToList(T[] a) {
         return Arrays.stream(a).collect(Collectors.toList());
    }

    // definition of a generic method
    public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
         return Arrays.stream(a).map(mapperFunction).collect(Collectors.toList());
    }

    // example of a generic method that has Number as an upper bound for T
    public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
         return Arrays.stream(a).collect(Collectors.toList());
    }

    public static void main(String[] args) {
         // testing the generic method with Integer
         Integer[] intArray = { 1, 2, 3, 4, 5 };
         List<Integer> list = fromArrayToList(intArray);
         list.forEach(element -> System.out.println(element));

         // testing the generic method with String
         String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
         List<String> strList = fromArrayToList(stringArray);
         strList.forEach(element -> System.out.println(element));

         // testing the generic method with Integer and String type
         Integer[] intArr = { 1, 2, 3, 4, 5 };
         List<String> stringList = fromArrayToList(intArr, Object::toString);
         stringList.forEach(element -> System.out.println(element));
     }
}
Output:
1
2
3
4
5
hello1
hello2
hello3
hello4
hello5
1
2
3
4
5

Create Generic equals and compare Method Example

Let's create Generic class GenType.
public class GenType<T> {
    private T t;

    public T get() {
         return this.t;
    }

    public void set(T t1) {
         this.t = t1;
    }
}
Let's write generic equals and compare methods to check equality.
public static <T> boolean isEqual(GenType<T> g1, GenType<T> g2){
     return g1.get().equals(g2.get());
}

public static <T extends Comparable<T>> int compare(T t1, T t2){
     return t1.compareTo(t2);
}
The complete example of the above generic methods and it's testing code.
public class GenericsMethods {

    //Generics in method
    public static <T> boolean isEqual(GenType<T> g1, GenType<T> g2){
         return g1.get().equals(g2.get());
    }
 
    public static <T extends Comparable<T>> int compare(T t1, T t2){
         return t1.compareTo(t2);
    }
 
    public static void main(String args[]){
         GenType<String> g1 = new GenType<>();
         g1.set("demo");
  
         GenType<String> g2 = new GenType<>();
         g2.set("demo");
  
         boolean isEqual = GenericsMethods.<String>isEqual(g1, g2);
  
         System.out.println(isEqual);
         //above statement can be written simply as
         isEqual = GenericsMethods.isEqual(g1, g2);
  
         System.out.println(isEqual);
  
         System.out.println(GenericsMethods.compare("abc","abc"));
    }
}
Output:
true
true
0

Related Java Generics Examples

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments