Java - From a String Type name how to initialize an ArrayList of that Type

sribasu

May be many of you have several times wanted to do this. Right now I am trying to do it but stuck.

Say, I have a method like this:

private Object getList(String nameofType) {
  return new ArrayList<Type>(); 
/**e.g. It returns ArrayList<java.lang.Double> if nameofType is "java.lang.Double", 
/*ArrayList<java.io.File> if nameofType is "java.io.File"**/
}

How can I init an ArrayList like this?

walen

The closest thing to what you want to do, might be this:

private List getList(String nameofType) {
    List list = null;
    try {
        Class clazz = Class.forName(nameofType); //must be fully qualified, example: "java.lang.Integer"
        list = Collections.checkedList(new ArrayList(), clazz);
    } catch (ClassNotFoundException e) {
        // log exception, etc.
    }
    return list;
}

This will return an effectively type-checked list, that will throw an exception if you try to insert an object of different type than specified.
You can check it four yourself:

    List list = getList("java.lang.Integer");
    System.out.println("Inserting Integer");
    list.add(new Integer(1));
    System.out.println("List: "+ list);
    System.out.println("Inserting Long");
    list.add(new Long(1));
    System.out.println("List: "+ list);

Output:

Inserting Integer
List: [1]
Inserting Long
Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Long element into collection with element type class java.lang.Integer

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add a User Input of type String to an ArrayList in Java?

From Dev

Cast to a type from the type name as a string

From Dev

JAVA: How can I initialize an ArrayList to specific class type with Object-wrapped instance?

From Dev

Initialize ArrayList of Type Abstract class

From Dev

Initialize own type with string

From Dev

Initialize own type with string

From Dev

How to print a specific object type from an abstract class arraylist in java

From Dev

Type mismatch: cannot convert from ArrayList<?> to ArrayList<String> in tomcat 7

From Dev

how to access the java arraylist type in R

From Dev

Java: how to retrieve a specific type in an arraylist?

From Dev

How to sort an arraylist with different data type in java

From Dev

how to access the java arraylist type in R

From Dev

Java: Sorting Map of Type Map<String, ArrayList<String>>

From Dev

How to get a type from a string?

From Dev

How to get type from String?

From Dev

How to get type from String?

From Dev

How to create ArrayList (ArrayList<T>) from array of primitive type elements

From Dev

How to create ArrayList (ArrayList<T>) from array of primitive type elements

From Dev

How do I parse ANY Number type from a String in Java?

From Dev

Getting variable type from variable name in Java

From Dev

Android:how to have an Array of type ArrayList<HashMap<String, String>>

From Dev

How to sort Hashmap of type Hashmap<String, Arraylist<String>> by value?

From Dev

XSD.exe - How to initialize a type created from xs:choice

From Dev

How can i initialize the Source Type from Lead to Opportunity

From Dev

Convert ArrayList of String type into Date type?

From Dev

Arraylist<Integer> and String from databean type mismatch exception

From Dev

Sorting multi-type string ArrayList in Java based on Integers

From Dev

How to properly initialize a var type

From Dev

how change type of ArrayList popped from stack from object to linkedlist?

Related Related

  1. 1

    How do I add a User Input of type String to an ArrayList in Java?

  2. 2

    Cast to a type from the type name as a string

  3. 3

    JAVA: How can I initialize an ArrayList to specific class type with Object-wrapped instance?

  4. 4

    Initialize ArrayList of Type Abstract class

  5. 5

    Initialize own type with string

  6. 6

    Initialize own type with string

  7. 7

    How to print a specific object type from an abstract class arraylist in java

  8. 8

    Type mismatch: cannot convert from ArrayList<?> to ArrayList<String> in tomcat 7

  9. 9

    how to access the java arraylist type in R

  10. 10

    Java: how to retrieve a specific type in an arraylist?

  11. 11

    How to sort an arraylist with different data type in java

  12. 12

    how to access the java arraylist type in R

  13. 13

    Java: Sorting Map of Type Map<String, ArrayList<String>>

  14. 14

    How to get a type from a string?

  15. 15

    How to get type from String?

  16. 16

    How to get type from String?

  17. 17

    How to create ArrayList (ArrayList<T>) from array of primitive type elements

  18. 18

    How to create ArrayList (ArrayList<T>) from array of primitive type elements

  19. 19

    How do I parse ANY Number type from a String in Java?

  20. 20

    Getting variable type from variable name in Java

  21. 21

    Android:how to have an Array of type ArrayList<HashMap<String, String>>

  22. 22

    How to sort Hashmap of type Hashmap<String, Arraylist<String>> by value?

  23. 23

    XSD.exe - How to initialize a type created from xs:choice

  24. 24

    How can i initialize the Source Type from Lead to Opportunity

  25. 25

    Convert ArrayList of String type into Date type?

  26. 26

    Arraylist<Integer> and String from databean type mismatch exception

  27. 27

    Sorting multi-type string ArrayList in Java based on Integers

  28. 28

    How to properly initialize a var type

  29. 29

    how change type of ArrayList popped from stack from object to linkedlist?

HotTag

Archive