Generics — Java 11

In this example, we’ll look at the Java 11 generics.

  • Implementing a generic type
public class AgeComparator implements Comparator<Person>
  • Type Bound
public class SortedPair<T extends Comparable<T>>
  • Passing Parameter to generic type
public class Reverser<T> implements Comparator<T>

Person.java

package com.example;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private String name;
private int age;
}

AgeComparator.java

package com.example;

import java.util.Comparator;

public class AgeComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return Integer.compare(o1.getAge(), o2.getAge());
}
}

Reverser.java

package com.example;

import java.util.Comparator;

public class Reverser<T> implements Comparator<T> {

private final Comparator<T> comparator;

public Reverser(Comparator<T> comparator) {
this.comparator = comparator;
}

@Override
public int compare(T left, T right) {
return comparator.compare(right, left);
}
}

MainApp.java

package com.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortingExample {
public static void main(String[] args) {
Person p1 = new Person("Neha Parate", 89);
Person p2 = new Person("Ashwini Dhapodkar", 65);
Person p3 = new Person("Shweta Nandankar", 100);

List<Person> personList = new ArrayList<>();
personList.add(p1);
personList.add(p2);
personList.add(p3);

Collections.sort(personList, new AgeComparator());
System.out.println(personList);

Collections.sort(personList, new Reverser<>(new AgeComparator()));
System.out.println(personList);
}
}

— — — — — — — — — — — — — — — — — — — — — — — — — —

SortedPair.java

package com.example;

import lombok.Data;

@Data
public class SortedPair<T extends Comparable<T>> {
private T first;
private T last;

public SortedPair(T left, T right) {
if(left.compareTo(right) < 0) {
this.first = left;
this.last = right;
}else{
this.first = right;
this.last = left;
}
}
}

SortedPairTest.java

package com.example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class SortedPairTest {
@Test
public void testShouldRetainOrderOfOrderedPair() {
SortedPair<Integer> pair = new SortedPair<>(1,2);
Assertions.assertEquals(1, pair.getFirst().intValue());
Assertions.assertEquals(2, pair.getLast().intValue());
}

@Test
public void testShouldFlipOrderOfMisorderedPair(){
SortedPair<Integer> pair = new SortedPair<>(2,1);
Assertions.assertEquals(1, pair.getFirst().intValue());
Assertions.assertEquals(2, pair.getLast().intValue());
}
}

=============================

Generics on method:

package com.example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class SortingExample {
public static void main(String[] args) {
Person p1 = new Person("Neha Parate", 89);
Person p2 = new Person("Ashwini Dhapodkar", 65);
Person p3 = new Person("Shweta Nandankar", 100);

List<Person> personList = new ArrayList<>();
personList.add(p1);
personList.add(p2);
personList.add(p3);

final Person youngPerson = min(personList, new AgeComparator());
System.out.println(youngPerson);

List<Integer> numbers = new ArrayList<>();
numbers.add(11);
numbers.add(12);
numbers.add(13);
System.out.println(min(numbers, Integer::compare));
}

private static <T> T min(List<T> values, Comparator<T> comparator) {
if(values.isEmpty()){
throw new IllegalArgumentException("List is empty, cannot find min");
}
T lowestElement = values.get(0);
for (int i = 0; i < values.size(); i++) {
final T element = values.get(i);
if(comparator.compare(element, lowestElement) < 0){
lowestElement = element;
}
}
return lowestElement;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
PA

PA

Java Developer and enthusiast