Checking if Object is instance of List<Object>
I was wrong in my previous answer since i not fully understood your requirements. if your added item is an Object you may add it without any problem as long as you Have a list of something. You don't have to recreate the list
void add(Object toAdd) {
Object obj = getObject();
if (obj instanceof List<?>) {
((List<Object>)obj).add(toAdd);
return;
}
throw new SomeException();
}
UPDATE
as answer to few comments, there is no problem to add any object to a list, and there is no problem to find out what type of object it is during iteration after it:
List<String> x1 = new ArrayList<String>();
Object c3 = x1;
x1.add("asdsad");
Integer y2 = new Integer(5);
if (c3 instanceof List<?>){
((List<Object>)c3).add((Object)y2);
}
for (Object i : (List<Object>)c3){
if (i instanceof String){
System.out.println("String: " + (String)i);
}
if (i instanceof Integer){
System.out.println("Integer: "+ (Integer)i);
}
}
Wolf480pl
Updated on February 05, 2020Comments
-
Wolf480pl about 4 years
I have an
Object
that sometimes contains aList<Object>
. I want to check it withinstanceof
, and if it is, then add some elements to it.void add(Object toAdd) { Object obj = getValue(); if (obj instanceof List<?>) { List<?> list = obj; if (list instanceof List<Object>) { // Error ((List<Object>) list).add(toAdd); } else { List<Object> newList = new ArrayList<Object>(list); newList.add(toAdd); setValue(newList); } return; } throw new SomeException(); }
And it says I can't check if it is
instanceof List<Object>
because java doesn't care and erased the type in<>
. Does this mean I have to create new ArrayList every time? Or is there a way to check that, eg. with reflection? -
Wolf480pl about 11 yearsI want to make sure the parameter is not a subclass of Object. Will getActualTypeArguments() == Object.class work?
-
Wolf480pl about 11 yearsAlso, is this going to be faster than copying a <50 elements ArrayList?
-
newacct about 11 yearsThis is completely wrong. This is only for if the object's class is a subclass of ArrayList or something, and which extends it with a specific type parameter. None of which is what the OP is doing.
-
Michael about 11 years@newacct, you are right and the answer have been edited, the answer was written before the question has been edited and explained more clearly, as regard to the subclass of ArrayList, you are also right, my mistake as i had time shortage. Although the hint for knowing the type of contained object through reflection and ParameterizedType is valuable.
-
Paul Bellora about 11 years-1 Your edit is still wrong. A
List<?>
is not necessarily aList<Object>
. -
Michael about 11 years@Paul Bellora, List<?> is necessarily a List of Objects, you can't create a List of primitives, give it a try.. everything else is Object
-
Paul Bellora about 11 yearsA
List<?>
could be aList<String>
and anObject
could be anInteger
. In this case you've added anInteger
to aList<String>
via an unchecked cast so it will only fail sometime later when theInteger
is accessed like aString
. Since the OP is assuming anyList<?>
must be aList<Object>
(I guess because of snakeyaml), you should change your answer to reflect that. "if your added item is an Object you may add it without any problem as long as you Have a list of something" is wrong. -
Michael about 11 years@Paul Bellora, the answer was edited to demonstrate to you how it will be easily accessed after it without any problem , it works! and a list of something is exactly the same as a list of object , there is no mistake here, OP has meant that its List can hold different object types