Hackerrank Day 21: Generics

Write a single generic function named printArray; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector). The locked Solution class in your editor tests your function.

Note: You must use generics to solve this challenge. Do not write overloaded functions.

Input Format

There is no input for this challenge. The locked Solution class in your editor will pass two different types of arrays to your printArray function.


  • You must have exactly 1 function named printArray.

Output Format

Your printArray function should print each element of its generic array parameter on a new line.

My Solution in Java 8:


I still don’t really understand Generics so I’m just going to put what I got from the internet here for future references.

What is Generics?

Generic constructs are a very efficient way of reusing your code. In the same way you have parameters for a function, generics parameterize type and allow you to use apply the same interface, class, or method using different data types while still restricting operations to that data type (meaning that you still get strong type checking at compile time).

public interface List<E> extends Collection<E>
public interface Map<K,V>

The letters enclosed between angle brackets (< and >) are type parameters and, like many things in programming, there is a convention behind them (remember, following conventions help us write clean, readable code!). The letters below are commonly-used generic type parameters:

  • E – Element
  • K – Key
  • V – Value
  • N – Number
  • T – Type (e.g.: data type)
  • S,U,V, etc. These are second, third, and fourth types for when T is already in use.

A parameterized type is basically a class or interface name that is immediately followed by a type parameter in angle brackets. Observe that List and Map are both parameterized types, and their respective parameters (E, K, and V) all follow the conventions shown above. This helps us make some assumptions about the type of objects these type parameters are standing in for.

Just like we pass arguments to functions and methods, we need to specify data types for our type parameters when we instantiate generic objects.

Once a data type is specified and an object is created, the specified type replaces every ocurrence of the generic type parameter in the instantiated class. The compiler also performs strict type checking to ensure you haven’t tried to do anything not allowable for that data type (e.g.: trying to add an element to integerList that isn’t of type Integer).

Why use Generics?

Code that uses generics has many benefits over non-generic code:

  • Stronger type checks at compile time.
    A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.
  • Elimination of casts.
    The following code snippet without generics requires casting:

    List list = new ArrayList();
    String s = (String) list.get(0);

    When re-written to use generics, the code does not require casting:

    List<String> list = new ArrayList<String>();
    String s = list.get(0);   // no cast
  • Enabling programmers to implement generic algorithms.
    By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.
Hackerrank Day 21: Generics