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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:16:13+00:00 2026-06-13T05:16:13+00:00

Is there any performance difference between HashMap and LinkedHashMap for traversal through values() function?

  • 0

Is there any performance difference between HashMap and LinkedHashMap for traversal through values() function?

  • 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-13T05:16:14+00:00Added an answer on June 13, 2026 at 5:16 am

    I think the LinkedHashMap has to be faster in traversal due to a superior nextEntry implementation in its Iterator

    Here is why :

    Let us go step by step from the values implementation.
    The HashMap implementation of values is this :

    public Collection<V> values() {
        Collection<V> vs = values;
        return (vs != null ? vs : (values = new Values()));
    }
    

    The LinkedHashMap extends from HashMap and inherits the same implementation.

    The difference is in the Iterator implementation for the Values in both.

    for HashMap it extends from java.util.HashMap.HashIterator

    private final class ValueIterator extends HashIterator<V> {
        public V next() {
            return nextEntry().value;
        }
    }
    

    but for LinkedHashMap it extends from java.util.LinkedHashMap.LinkedHashIterator

    private class ValueIterator extends LinkedHashIterator<V> {
        public V next() { return nextEntry().value; }
    }
    

    so the difference essentially boils down to nextEntry implementation.

    For LinkedHashMap it is just calling e.after where e is the Entry ,
    but for HashMap there is some work involved in traversing the Entry[] array to find the next next.

    UPDATE : Code for nextEntry() in HashMap

    final Entry<K,V> nextEntry() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        Entry<K,V> e = next;
        if (e == null)
            throw new NoSuchElementException();
    
        if ((next = e.next) == null) {
            Entry[] t = table;
            while (index < t.length && (next = t[index++]) == null)
                ;
        }
        current = e;
        return e;
    }
    

    The Entry[] is not a contiguous store. (There could be null values in between). If you take a look at the above code, what it does is point next to current and find the next next by iterating over the Entry[] .

    But I think this performance gain will come at the cost of insertion. Check out the addEntry method in both classes as an exercise.

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

Sidebar

Related Questions

Is there any performance difference between: p { margin:0px; padding:0px; } and omitting the
Are there any performance difference between decimal(10,0) unsigned type and int(10) unsigned type?
I was wondering if there was any performance difference between the two approaches below.
I would like to know is there any difference in performance between these two
Is there any difference in performance ( speed wise ) between a synchronous request
Is there any difference in performance - or otherwise - between: ptr->a(); and (*ptr).a();
Is there any difference in (asymptotic) performance between var a = Orders.OrderBy(order => order.Date).First()
Is there any performance difference between using int a=a+1 and a++ in Java ?
Is there any performance difference between dbms_lob.instr and contains or am I doing something
Is there any performance difference between varchar(1) and char(1) ? Which RDBMS handle these

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.