Contoh Generic Java

Run Settings
LanguageJava
Language Version
Run Command
import java.util.*; class Main { public static void main(String[] args) { // soal Collection<Integer> bilangan = List.of(5, 72, 3, 58, 88, 23, 10); Collection<String> mahasiswa = List.of("Adi", "Budi", "Cindy", "Dodi"); // cara 1: // Printer<Integer> printerInteger = new Printer<>(); // printerInteger.print(bilangan); // Printer<String> printerString = new Printer<>(); // printerString.print(mahasiswa); // cara 2: // Printer<Integer, String> printer = new Printer<>(); // printer.print(bilangan, mahasiswa); // cara 3: Printer<Integer, String> printer = new Printer<>(bilangan, mahasiswa); } } // cara 1: // class Printer<T> { // public <T> void print(T obj) { // System.out.println(obj); // } // } // cara 2: // class Printer<T, U> { // public <T, U> void print(T obj1, U obj) { // System.out.println(obj1); // System.out.println(obj2); // } // } // cara 3: class Printer<T, U> { public <T, U> Printer(T obj1, U obj2) { System.out.println(obj1); System.out.println(obj2); } }
Editor Settings
Theme
Key bindings
Full width
Lines