How do I know what type of each object is in an ArrayList <Object>?

I have an ArrayList consisting of different elements imported from db consisting of strings, numbers, paired and ints. Is there a way to use the reflection technique to find out that each data type has every element?

FYI: The reason that there are so many data types is because it is part of the Java code written for implementation with different databases.

+87
java arraylist generics reflection
Sep 19 '08 at 23:17
source share
12 answers

In C #:
Corrected by Mike's recommendation

ArrayList list = ...; // List<object> list = ...; foreach (object o in list) { if (o is int) { HandleInt((int)o); } else if (o is string) { HandleString((string)o); } ... } 

In Java:

 ArrayList<Object> list = ...; for (Object o : list) { if (o instanceof Integer)) { handleInt((Integer o).intValue()); } else if (o instanceof String)) { handleString((String)o); } ... } 
+99
Sep 19 '08 at 23:20
source share

You can use the getClass() method, or you can use instanceof. for example

 for (Object obj : list) { if (obj instanceof String) { ... } } 

or

 for (Object obj : list) { if (obj.getClass().equals(String.class)) { ... } } 

Note that instanceof will correspond to subclasses. For example, from C is a subclass of A , then the following will be done:

 C c = new C(); assert c instanceof A; 

However, the following will be false:

 C c = new C(); assert !c.getClass().equals(A.class) 
+54
Sep 19 '08 at 23:28
source share
 for (Object object : list) { System.out.println(object.getClass().getName()); } 
+45
Sep 19 '08 at 23:23
source share

You almost never want you to use something like:

 Object o = ... if (o.getClass().equals(Foo.class)) { ... } 

because you do not consider possible subclasses. You really want to use Class # isAssignableFrom:

 Object o = ... if (Foo.class.isAssignableFrom(o)) { ... } 
+13
Sep 21 '08 at 0:04
source share

In Java, just use the instanceof operator. It will also take care of subclasses.

 ArrayList<Object> listOfObjects = new ArrayList<Object>(); for(Object obj: listOfObjects){ if(obj instanceof String){ }else if(obj instanceof Integer){ }etc... } 
+5
Oct. 13 '11 at 2:35 a.m.
source share
 import java.util.ArrayList; /** * @author potter * */ public class storeAny { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Object> anyTy=new ArrayList<Object>(); anyTy.add(new Integer(1)); anyTy.add(new String("Jesus")); anyTy.add(new Double(12.88)); anyTy.add(new Double(12.89)); anyTy.add(new Double(12.84)); anyTy.add(new Double(12.82)); for (Object o : anyTy) { if(o instanceof String){ System.out.println(o.toString()); } else if(o instanceof Integer) { System.out.println(o.toString()); } else if(o instanceof Double) { System.out.println(o.toString()); } } } } 
+5
Feb 15 '13 at 14:42
source share

Just call .getClass() for each Object in the loop.

Unfortunately, Java does not have map() . :)

+4
Sep 19 '08 at 23:20
source share

Instanceof works if you are not dependent on specific classes, but also remember that you can have zeros in the list, so obj.getClass () will fail, but instanceof always returns false to null.

+3
Sep 20 '08 at 0:04
source share

Since Java 8

 mixedArrayList.forEach((o) -> { String type = o.getClass().getSimpleName(); switch (type) { case "String": // treat as a String break; case "Integer": // treat as an int break; case "Double": // treat as a double break; ... default: // whatever } }); 
+3
May 10 '15 at 11:12
source share

instead of object.getClass().getName() you can use object.getClass().getSimpleName() because it returns a simple class name without the package name included.

eg,

 Object[] intArray = { 1 }; for (Object object : intArray) { System.out.println(object.getClass().getName()); System.out.println(object.getClass().getSimpleName()); } 

gives

 java.lang.Integer Integer 
+2
Feb 05 '15 at 18:06
source share

You say, β€œThis is a piece of written Java code,” from which I conclude that there is still a chance that you could design it differently.

Having an ArrayList is like a collection of materials. Instead of forcing instanceof or getClass every time you take an object from the list, why not create a system so that you get the type of the object when you extract it from the database and save it in the collection of the corresponding type of object?

Or you can use one of the many data access libraries that exist for you.

0
Sep 20 '08 at 1:11
source share

If you expect the data to be numerically in any form, and all that interests you is to convert the result to a numerical value, I would suggest:

 for (Object o:list) { Double.parseDouble(o.toString); } 
0
Dec 11 '08 at 14:56
source share



All Articles