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 8443893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:15:41+00:00 2026-06-10T09:15:41+00:00

I have someMethod() which returns Collection<Long> . ArrayList<Long> results = (ArrayList<Long>) someMethod(); Long value

  • 0

I have someMethod() which returns Collection<Long>.

ArrayList<Long> results = (ArrayList<Long>) someMethod();
Long value = results.get(0);

I get ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long.
Trying System.out.println(results.get(0)); in fact, it returns e.g. [32].
I do not understand this. This is ArrayList of Long! Why get(0) returns ArrayList?

This helps:

Object o = results.get(0);
ArrayList<Long> al = (ArrayList<Long>) o;
Long val = al.get(0);

but why this is needed?

  • 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-06-10T09:15:42+00:00Added an answer on June 10, 2026 at 9:15 am

    Clearly someMethod() is actually returning Collection<List<Long>>, violating declared return type. This is possible due to type erasure. The compiler is happy as long as the class type is correct, generic type can be ignored (the compiler will only raise warning). Basically this is a bug in someMethod().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get a regex which will match a tag in a java
I have a base class that has an abstract method which returns a list
I have a public static method string SomeMethod(string message) inside a SomeClass class which
I have a java swing application which expects the users to choose YES or
Suppose I have a given object of type IEnumerable<string> which is the return value
I have two objects. Object A and B. A has a method which returns
I have a method which returns me some object let say a String object.
I have a controller with a method which returns a partial view and generates
For instance I have a method SomeMethod(Graphics g) { ... } If I will
I have this public Result SomeMethod() { Popup popup = new Popup(); popup.Closed +=

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.