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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:40:41+00:00 2026-06-02T07:40:41+00:00

My question is regarding optimization in java using the Android compiler. Will map.values() in

  • 0

My question is regarding optimization in java using the Android compiler. Will map.values() in the following be called every iteration, or will the Android compiler optimize it out.

LinkedHashMap<String, Object> map;

for (Object object : map.values())
{
   //do something with object
}

Likewise here is another example. will aList.size() be called every iteration?

List<Object> aList;

for (int i = 0; i < aList.size(); i++)
{
    object = aList.get(i);
    //do something with i
}

And after all this, does it really matter if it calls the methods every iteration? Does Map.values(), and List.size() do much of anything?

  • 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-02T07:40:43+00:00Added an answer on June 2, 2026 at 7:40 am

    In the first example, map.values() will be evaluated once. According to the Section 14.4.2 of the Java Language Specification, it is equivalent to:

    for (Iterator<Object> i = map.values().iterator(); i.hasNext(); ) {
        Object object = i.next();
        // do something with object
    }
    

    In the second, aList.size() will be called every time the test is evaluated. For readability, it would be better to code it as:

    for (Object object : aList) {
        // do something with object
    }
    

    However, per the Android docs (on a page no longer available that I could find, but archived here) this will be slower. Assuming that you aren’t changing the list size inside the loop, the fastest another way would be to pull out the list size ahead of the loop:

    final int size = aList.size();
    for (int i = 0; i < size; i++)
    {
        object = aList.get(i);
        //do something with i
    }
    

    This will be substantially faster (the Android docs linked to above say by a factor of 3) if aList happens to be an ArrayList, but is likely to be slower (possibly by a lot) for a LinkedList. It all depends on exactly what kind of List implementation class aList is.

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

Sidebar

Related Questions

Quick question regarding memory management in C++ If I do the following operation: pointer
I have this question in mind regarding optimazation of compiler when it comes to
EDIT: Optimization results at end of this question! hi, i have a following code
I've got a question regarding multiple-thread method invocation in Java. Let's say we have
Quick question regarding doing a grep with xargs. When I do it, I will
This is a rather academic question, I realise it matters little regarding optimization, but
Question regarding redirection using htaccess. On a GET search form submit I get the
I have a question about structure padding and memory alignment optimizations regarding structures in
Question regarding GtkBuilder. When we unref builder pointer does it destroys all the screens/widgets
Question regarding fluent nHibernate table mappings: Apparently I was under the mistaken impression, that

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.