How do I identify object types in JAVA ArrayList?

Asked 2 years ago, Updated 2 years ago, 32 views

Can I check if the type specified in ArrayList is the same as the object?
For example, ArrayList<ABC>abc and ABC are of the same type.

I tried the methods instanceOf and isAssignableFrom, but it didn't work.
Thank you for your cooperation.

java

2022-09-30 10:31

3 Answers

As for the ArrayListabc and ABC comparison, there is no need to compare the ArrayList and ABC classes because they are different classes, but we can verify them next time.

 if (abc instance of ABC) {
  // Action if true
} else{
  // other than that
}

If you want to compare "stored" objects in the ArrayList to see if there is an ABC class, you can do the following:

List list = new ArrayList();
list.add(newABC());
list.add(new XYZ());

for (Object objectj:list) {
  if(obj instance of ABC){
    // ABC Case Handling ABC
  } else{
    // other than that
  }
}


2022-09-30 10:31

Answer the question by thinking that you want to determine if the type T of an object is the same type as the type variable you specified in the generic (for example, List<T>).

For example, specifying a type variable such as ArrayList<String> ensures that the object type handled by ArrayList is String.

Therefore, if you write ArrayList<ABC> on the code, it is obvious that the ArrayList is bound to the ABC type, so you should simply determine if any object is of the ABC type.

Object object;//Assume that there is a mysterious object that is not sure if it is the actual type.
if (object instance of ABC) { 
    // You can use the instanceof operator to determine if it is ABC type.
}

If the list has elements, you can say the following, but there is no direct way to know what type variables are.

String object="ABC";
List<String>list=new ArrayList<String>();;
list.add("DEF");
if(list.get(0).getClass().isAssignableFrom(object.getClass())){
    System.out.println("true");
}

Java generics are implemented by type erasure.That's why List<String> has no way of knowing the String type directly.

Although ArrayList<ABC> and ArrayList<DEF> are treated differently on the source code, the information is lost during compilation (type information is erased, so type erased), and both are considered just ArrayList.

Therefore, it is not possible to know the type variable type at runtime.

However, the above description is not strictly accurate, and if a class field or method argument is generic, for example,

private List<String>hoge;

public void hoge (List<String>fuga) {

}

Type information for hoge and fuga above is also retained at runtime.

You can get the ParameterizedType using reflection, but I think it's out of the questioner's mind, so there are some of them.


2022-09-30 10:31

I cannot read the processing result from the text, but since the type ArrayList and the type ABC are different, it seems that the processing such as instanceOf is not the same.
(If the ABC type implements the List class or inherits ArrayList, it will be a little different...)

ArrayList<ABC>list=newArrayList<ABC>();;
 if((list insetanceOfABC)==false){
    System.out.println("Not the same type.");
 }


2022-09-30 10:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.