How do I find out what type each object is in a ArrayList<Object>?

267,706

Solution 1

In C#:
Fixed with recommendation from Mike

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);
    }
    ...
}

Solution 2

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 match subclasses. For instance, of C is a subclass of A, then the following will be true:

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

However, the following will be false:

C c = new C();
assert !c.getClass().equals(A.class)

Solution 3

for (Object object : list) {
    System.out.println(object.getClass().getName());
}

Solution 4

You almost never want you use something like:

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

because you aren't accounting for possible subclasses. You really want to use Class#isAssignableFrom:

Object o = ...
if (Foo.class.isAssignableFrom(o)) {
    ...
}

Solution 5

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());
            }
        }
    }
}
Share:
267,706
WolfmanDragon
Author by

WolfmanDragon

Java, Web and PostgreSQL

Updated on June 22, 2020

Comments

  • WolfmanDragon
    WolfmanDragon almost 4 years

    I have a ArrayList made up of different elements imported from a db, made up of strings, numbers, doubles and ints. Is there a way to use a reflection type technique to find out what each type of data each element holds?

    FYI: The reason that there is so many types of data is that this is a piece of java code being written to be implemented with different DB's.

  • Michael Brown
    Michael Brown over 15 years
    actually instead of using o.GetType()==typeof(int)) just say if(o is int);
  • John Gardner
    John Gardner over 15 years
    don't forget about null if it is possible in your list. You'll get NullPointerExceptions from this example with nulls.
  • Neil
    Neil over 15 years
    And if you happen to be worried about every nanosecond, "as" will save you a few compared to "is" with a cast.
  • Razor Storm
    Razor Storm over 12 years
    Can't you just do instanceof in the java case?
  • alan turing
    alan turing over 9 years
    For Integer case, it should also be Integer.class, I just tried Integer.TYPE does not work.
  • Max
    Max almost 5 years
    (int) o doesn't work in Java. It produces the error message Cannot cast from Object to int. Use (Integer o).intValue() instead.