Generics in Minitab
import java.util.ArrayList;
import java.util.List;
public class Generics {
// As an example of a generic method, slicesIndex takes
// a list of any comparable type and an element of that
// type and returns the index of the first occurrence of
// v in s, or -1 if not present.
public static <T extends Comparable<T>> int slicesIndex(List<T> s, T v) {
for (int i = 0; i < s.size(); i++) {
if (v.compareTo(s.get(i)) == 0) {
return i;
}
}
return -1;
}
// As an example of a generic type, LinkedList is a
// singly-linked list with values of any type.
public static class LinkedList<T> {
private Node<T> head, tail;
private static class Node<T> {
T val;
Node<T> next;
Node(T val) {
this.val = val;
}
}
// We can define methods on generic types just like we
// do on regular types.
public void push(T v) {
Node<T> newNode = new Node<>(v);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
// AllElements returns all the LinkedList elements as a list.
public List<T> allElements() {
List<T> elements = new ArrayList<>();
Node<T> current = head;
while (current != null) {
elements.add(current.val);
current = current.next;
}
return elements;
}
}
public static void main(String[] args) {
List<String> s = List.of("foo", "bar", "zoo");
// When invoking generic methods, we can often rely
// on type inference. Note that we don't have to
// specify the type for T when calling slicesIndex
// - the compiler infers it automatically.
System.out.println("index of zoo: " + slicesIndex(s, "zoo"));
// ... though we could also specify it explicitly.
slicesIndex(s, "zoo");
LinkedList<Integer> lst = new LinkedList<>();
lst.push(10);
lst.push(13);
lst.push(23);
System.out.println("list: " + lst.allElements());
}
}This Java code demonstrates the use of generics, which is similar to the concept of type parameters in the original example. Here’s a breakdown of the changes and explanations:
The
SlicesIndexfunction is transformed into a static generic methodslicesIndex. It uses a type parameterTthat extendsComparable<T>to ensure the elements can be compared.The
Listtype is implemented as a genericLinkedListclass. It uses a nestedNodeclass to represent list elements.The
Pushmethod is renamed topushto follow Java naming conventions.The
AllElementsmethod is renamed toallElementsand returns aList<T>instead of an array.In the
mainmethod, we create an immutable list usingList.of()instead of an array literal.The example demonstrates both type inference and explicit type specification when calling the generic method.
The output is generated using
System.out.println()instead offmt.Println().
To run this program, save it as Generics.java, compile it with javac Generics.java, and then run it with java Generics. The output will be:
index of zoo: 2
list: [10, 13, 23]This example showcases how Java implements generics, which provide type safety and code reuse, similar to the generics feature in the original example.