Java Generics: An Example

Here are two questions that I found in https://docs.oracle.com/javase/tutorial/java/generics:

Q1. Write a generic method to count the number of elements in a collection that have a specific property (for example, odd integers, even numbers, prime numbers, palindromes .. etc etc etc).

Q2. Write a generic method to exchange the positions of two different elements in an array.

Combinedly I have tried to solve these two as follows:

For Q1


Step 1:

An interface called SomeSpecialNumber is created:

SomeSpecialNumbe.java

public interface SomeSpecialNumber<T> {
public boolean match(T t);
}

The idea behind using this interface is such that we can implement it in our customized classes way ie. every special type of class (PrimeNumber class or EvenNumber class) that implements the ‘SomeSpecialNumber’ class, will be responsible for its own implementation of the match method.

Step 2:

Two implementation classes are created: PrimeNumber and EvenNumber

PrimeNumber.java

public class PrimeNumber implements SomeSpecialNumber<Integer> {

@Override
public boolean match(Integer t) {
for (int i = 2; i < t; ++i) {
if (t % i == 0) {
return false;
}
}
return true;
}
}

 

EvenNumber.java

public class EvenNumber implements SomeSpecialNumber<Integer> {

@Override
public boolean match(Integer t) {
return (t % 2 == 0);
}
}

As can be seen, the above two classes have their own way of implementing the ‘match’ method to count the prime numbers or even numbers.

Step 3:

A class called Algorithm is created to act as an intermediary or facade class to perform the count based on the supplied class as parameter

Algorithm.java

import java.util.List;

public class Algorithm {

public <T> int count(List<T> list, SomeSpecialNumber<T> s) {
int count = 0;
for (T type : list) {
if (s.match(type)) {
++count;
}
}
return count;
}
}

Step 4:

To test, let’s write a simple class as follows:

Test.java

public class Test {

public static void main(String[] args) {
Test tq = new Test();
tq.testSpecificElement();
}

public void testSpecificElement() {
List<Integer> list = Arrays.asList(1, 4, 5, 7, 8, 9, 13, 11);
PrimeNumber pn = new PrimeNumber();
EvenNumber en = new EvenNumber();
Algorithm a = new Algorithm();
System.out.println(” Count of prime numbers: ” + a.count(list, pn));
System.out.println(” Count of even numbers: ” + a.count(list, en));
}

}

 

After compiling/running Test.java, the following output is shown:

Result:

Count of prime numbers: 5
Count of even numbers: 2

For Q2


For the next part (swap), I simply added another method within the Test class, as follows:

private static <T> void swap(T[] a, int i, int j)  {
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}

 

To test it, the following block of code is added within the main method:

public static void main(String[] args) {
Test  tq = new Test();
Integer[] a = { 11, 22, 33, 44, 55 };
System.out.println(“Initial value: ” + a[0] + “, ” + a[3]);
swap(a, 0, 3);
System.out.println(“After swap value: ” + a[0] + “, ” + a[3]);
}

The Output:

Initial value: 11, 44
After swap value: 44, 11

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s