jeudi 28 janvier 2021

Converting String to Generics type argument in Java

I would like to write a Generic program that receives a String from the user and creates a container of that type using reflection. This should be something on the lines of

public static void main(String []args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the type you want to use ");
    String typeName = scanner.nextLine();
    Set<????> mySet = new HashSet(); 
}

What should I put in the ???? part? Is this possible?





Aucun commentaire:

Enregistrer un commentaire