w3resource

Java Generic Methods - Exercises, Practices, Solutions

Java Generic Method Exercises [7 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

Generic Methods:

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

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

1. Write a Java program to create a generic method that takes two arrays of the same type and checks if they have the same elements in the same order.

Click me to see the solution

2. Write a Java program to create a generic method that takes a list of numbers and returns the sum of all the even and odd numbers.

Click me to see the solution

3. Write a Java program to create a generic method that takes a list of any type and a target element. It returns the index of the first occurrence of the target element in the list. Return -1 if the target element cannot be found.

Click me to see the solution

4. Write a Java program to create a generic method that takes a list of any type and returns it as a new list with the elements in reverse order.

Click me to see the solution

5. Write a Java program to create a generic method that takes two lists of the same type and merges them into a single list. This method alternates the elements of each list.

Click me to see the solution

6. Write a Java program to create a generic method that takes a list of any type and a predicate. It returns an array list containing only elements that satisfy the predicate.

Click me to see the solution

7. Write a Java program to create a generic method that takes a map of any type and prints each key-value pair.

Click me to see the solution

Java Code Editor

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/generic/index.php