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

  • Home
  • SEARCH
  • 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 6539611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:52:44+00:00 2026-05-25T10:52:44+00:00

When I write some API, it sometimes will use Collection<Model> to be the parameter.

  • 0

When I write some API, it sometimes will use Collection<Model> to be the parameter. Of course, you can use ArrayList if you know ArrayList is already enough to handle all the use case.

My question is is there any considerable performance cost when for example cast the ArrayList<Model> to Collection<Model> when passing parameter.

Will the collection size also impact the performance of casting? Any advice?

Thanks for Peter’s answer.

I think the answer is pretty enough to stop me to waste time on changing it.

EDIT

As said in accepted answer, the cost is actually paid in the calling of interface methods.
it’s not free to keep this kind of flexibity. But the cost is not so considerable.

  • 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-25T10:52:45+00:00Added an answer on May 25, 2026 at 10:52 am

    Like most performance questions the answer is; write cleare and simple code and the application usually performs okay as well.

    A cast to an interface can take around 10 ns (less than a method call) Depending on how the code is optimised, it might be too small to measure.

    A cast between generic types is a compiler time check, nothing actually happens at runtime.

    When you cast, it is the reference type which changes, all references are the same size. The size of what they point to doesn’t matter.

    BTW: All ArrayList objects are the same size, All LinkedList objects are the same size all HashMap objects are the same size etc. They can reference an array which can be different sizes in different collection.


    You can see a difference in code which hasn’t been JITed.

    public static void main(String... args) throws Throwable {
      ArrayList<Integer> ints = new ArrayList<>();
      for(int i=0;i<100;i++) ints.add(i);
      sumSize(ints, 5000);
      castSumSize(ints, 5000);
      sumSize(ints, 5000);
      castSumSize(ints, 5000);
    }
    
    public static long sumSize(ArrayList<Integer> ints, int runs) {
      long sum = 0;
      long start = System.nanoTime();
      for(int i=0;i<runs;i++)
        sum += ints.size();
      long time = System.nanoTime() - start;
      System.out.printf("sumSize: Took an average of %,d ns%n", time/runs);
      return sum;
    }
    
    public static long castSumSize(ArrayList<Integer> ints, int runs) {
      long sum = 0;
      long start = System.nanoTime();
      for(int i=0;i<runs;i++)
        sum += ((Collection) ints).size();
      long time = System.nanoTime() - start;
      System.out.printf("castSumSize: Took an average of %,d ns%n", time/runs);
      return sum;
    }
    

    prints

    sumSize: Took an average of 31 ns
    castSumSize: Took an average of 37 ns
    sumSize: Took an average of 28 ns
    castSumSize: Took an average of 34 ns
    

    however the difference is likely to be due to the method calls being more expensive. The only bytecode difference is

    invokevirtual   #9; //Method java/util/ArrayList.size:()I
    

    and

    invokeinterface #15,  1; //InterfaceMethod java/util/Collection.size:()I
    

    Once the JIT has optimised the code there isn’t much difference. Run long enough, the time drops to 0 ns for the -server JVM because it detects the loop doesn’t do anything. 😉

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

Sidebar

Related Questions

I like to know how to write a program which would export some API
I want to write some JavaScript that will change the onmousedown of a div
I'm playing around, trying to write some code to use the tr.im APIs to
I'm trying to write some Thread Management utility, and reading up on ThreadMXBean. http://download.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#dumpAllThreads(boolean,
I am planing to write some apps with Google+. Where can I find its
I'm trying to write some PHP to upload a file to a folder on
I am looking to write some C# code for linux/windows/mac/any other platform, and am
I'm trying to write some C# code to get to a specific folder in
I would like to write some data to a file in Ruby. What is
I would like to write some scripts in python that do some automated changes

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.