Java Generics | HackerRank


Java Generics is a feature introduced in Java 5 that allows you to write classes and methods that operate on types that are specified as parameters when the class or method is instantiated or called. Generics provide a way to create reusable components that can work with any data type, thus improving code reusability, type safety, and reducing code duplication.

Generics allow you to write generic algorithms and data structures that can work with any type. This promotes code reusability as you can write code once and use it with different types without modification.


Code :


import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
    List s = new ArrayList();
    s.add(1);
    s.add(2);
    s.add(3);
    s.add("Hello");
    s.add("World");
   for(Object i : s){
        System.out.println(i);
    }
}
}



 

No comments: