In Java, you can check if an object is of a collection type (i.e., whether it implements the java.util.Collection interface) using the instanceof operator. The Collection interface is a common interface implemented by classes such as ArrayList, LinkedList, HashSet, and others. Here's how you can check if an object is a collection:
public class CheckCollectionType { public static void main(String[] args) { Object obj1 = new ArrayList<>(); Object obj2 = new HashSet<>(); Object obj3 = "This is a string"; if (obj1 instanceof Collection<?>) { System.out.println("obj1 is a collection."); } else { System.out.println("obj1 is not a collection."); } if (obj2 instanceof Collection<?>) { System.out.println("obj2 is a collection."); } else { System.out.println("obj2 is not a collection."); } if (obj3 instanceof Collection<?>) { System.out.println("obj3 is a collection."); } else { System.out.println("obj3 is not a collection."); } } } In this example:
obj1 is an instance of ArrayList, which implements the Collection interface, so the first if block prints "obj1 is a collection."
obj2 is an instance of HashSet, which also implements the Collection interface, so the second if block prints "obj2 is a collection."
obj3 is a string and does not implement the Collection interface, so the third if block prints "obj3 is not a collection."
You can use this instanceof approach to check if an object is a collection type before performing collection-specific operations on it.
uiimageview proxy image-editing asp.net-mvc-2 user-interface audio xcopy android-checkbox form-fields android-multidex