Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:13:31+00:00 2026-05-24T11:13:31+00:00

How can I tell what type of class is inside a Collection? I need

  • 0

How can I tell what type of class is inside a Collection? I need to handle simple data types differently than complex types and therefore I must know the contained class. At the moment, I have to iterate through the collection to find out the class which doesn’t sound right. This link was quite helpful but didn’t fully address my issue. Basically, here are the questions:
1. How to determine the class inside the collection?
2. how can I tell if the object is a java wrapper class (Integer, String, Date, etc..) or a proprietary class (Student, Vehicle, etc…).

Thank you
Jabawaba

entity = new SomeObject();
Class entityClass = entity.getClass();
for (Method method : entityClass.getDeclaredMethods()) {
  Class<?> returnType = method.getReturnType();
  if (returnType != null) {
    if (returnType.isPrimitive() || (returnType.getName().startsWith("java.lang.")) || (returnType == java.util.Date.class)) {
      // handling primitive and wrapper classes
      handleScalar(method);
    } else if (Collection.class.isAssignableFrom(returnType)) {
      Collection collection = (Collection) method.invoke(entity);
      if (collection == null || collection.isEmpty()) {
        continue;
      }
      for (Object value : collection) {
        if (value.getClass().getName().startsWith("java.lang.") || (value.getClass() == java.util.Date.class)) {
          handleSimpleVector(method);
          // no need to go through all the simple values
          continue;
        } else {
          // Each 'value' is itself a complex object.
          handleComplexObject(method);
        }
      }
    } else if (<return-type-is-an-Array>){
      // do something similar as the above.
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:13:32+00:00Added an answer on May 24, 2026 at 11:13 am

    1) You simply can’t if it’s not a generically typed Collection. Non-typed Collections can contain any mixture of object types.

    If you’re dealing with a (non-empty) typed Collection, then you could determine the class of its contents at runtime e.g. by

    Collection<?> coll = ...
    if (coll.isEmpty())
        throw new BadLuckException();
    Object representative = coll.iterator().next();
    Class<?> typeOfContent = representative.getClass();
    //apply your cases on this Class object
    

    There’s no other way to determine the generic type information at runtime since the generic type is purely compile-time information.

    2) Date is not a wrapper class – the primitive classes and their wrappers are:

    • short / Short
    • int / Integer
    • long / Long
    • byte / Byte
    • char / Char
    • double / Double
    • float / Float
    • boolean / Boolean

    You can determine whether your class is a primitive one using Class#isPrimitive. Note, however, that this will only determine whether an object is really primitive, it will not determine whether you are dealing with an autoboxed value:

    System.out.println(Integer.class.isPrimitive()); // -> false
    

    But, every wrapper class declares a field named “TYPE” that represents the primitive class it is wrapping. So you can check whether a class is a wrapper class like this:

    public static boolean isWrapperClass(Class<?> clazz) {
        try {
            clazz.getDeclaredField("TYPE");
            return true;
        catch (NoSuchFieldException ex) {
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone please tell me how to extract this kind of data : [{number:8457215152,type:Cell,state:LA,country:US,tz:CT,zip:70546,msa:0},{number:4363685555,type:Cell,state:LA,country:US,tz:CT,zip:70546,msa:0}]
Well, I'm completely new to JavaScript. Can you please tell me what type of
can you just tell me for which type of requirement we should choose framework
Can GHC or some lint tool tell me when I've provided a type signature
Anyone can tell me what I'm doing wrong? I am creating a simple system
Issue about Storeing unknown Data in a Class How can I provide a class
Consider this simple binary tree type: abstract class BinaryTree[T] { type Repr <: BinaryTree[T]
A hypothetical variadic template tuple class would, as far as I can tell, have
As far as I can tell, any static member of a class like String
The types of symbols class A[_] or of def a[_](x: Any) have a type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.