Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default HashMap vs LinkedHashMap

    Quoting the SCJP book:
    Although LinkedHashMap will be somewhat slower than HashMap for adding and removing elements, you can expect faster iteration with LinkedHashMap

    Java Code:
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    
    public class compare {
    	public static void main(String args[])
    	{
    		Map<Integer, Integer> hmap = new HashMap<Integer, Integer>();
    		Map<Integer, Integer> linkedhmap = new LinkedHashMap<Integer, Integer>();
    		
    		long startTime = System.nanoTime();
    		 
    		for (int i = 0; i < 100000; i++) {
    			hmap.put(i,i);
    		}
    		long endTime = System.nanoTime();
    		long duration = endTime - startTime;
    		System.out.println("Hashmap put:       " + duration);
    		 
    		// LinkedList add
    		startTime = System.nanoTime();
    		 
    		for (int i = 0; i < 100000; i++) {
    			linkedhmap.put(i,i);
    		}
    		endTime = System.nanoTime();
    		duration = endTime - startTime;
    		System.out.println("LinkedHashMap put: " + duration);
    		
    		
    		 startTime = System.nanoTime();
    		 
    		for (int i = 0; i < 10000; i++) {
    			hmap.get(i);
    		}
    		
    		endTime = System.nanoTime();
    		duration = endTime - startTime;
    		System.out.println("HashMap get:       " + duration);
    		 
    		// LinkedList get
    		startTime = System.nanoTime();
    		 
    		for (int i = 0; i < 10000; i++) {
    			linkedhmap.get(i);
    		}
    		endTime = System.nanoTime();
    		duration = endTime - startTime;
    		System.out.println("LinkedHashMap get: " + duration);
    		
    		startTime = System.nanoTime();
    		 
    		for (int i = 9999; i >=0; i--) {
    			hmap.remove(i);
    		}
    		endTime = System.nanoTime();
    		duration = endTime - startTime;
    		System.out.println("HashMap remove:       " + duration);
    		 
    		 
    		 
    		// LinkedList remove
    		startTime = System.nanoTime();
    		 
    		for (int i = 9999; i >=0; i--) {
    			linkedhmap.remove(i);
    		}
    		endTime = System.nanoTime();
    		duration = endTime - startTime;
    		System.out.println("LinkedHashMap remove: " + duration);
    	}
    }
    Output:
    Hashmap put:******65323798
    LinkedHashMap put:* 40719149
    HashMap get:******6414517
    LinkedHashMap get:* 31750320
    HashMap remove:******4345040
    LinkedHashMap remove:* 68173692

    According to the output while inserting LinkedHashMap is faster
    while iterating hashMap is faster
    while deleting hashmap is faster

    The above statements contradict with what is quoted in the book. What is going on?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default Re: HashMap vs LinkedHashMap

    To start with, I get just the opposite results that you get. But either way, your performance test is probably inadequate. I presume that one can find any particular set of data that would disagree with what was stated in the book. I do not know if what the book says is true or not. But I suspect they are talking about what would be expected over a wide range of key/value types.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: HashMap vs LinkedHashMap

    Somebody tell me if what is stated in the book is true or false because I seem to be getting different results which do not concur with the book.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: HashMap vs LinkedHashMap

    Reverse the order.
    That is, do the linkedhashmap "put" timing before the HashMap "put" timing.

    Indeed look at this:
    Java Code:
    public class TestThings {
        public static void main(String[] args) {
            System.out.println("PASS 1 ##########");
            timingMap();
            System.out.println("PASS 2 ##########");
            timingMap();
        }
    
        private static void timingMap() {
            HashMap<SomeBean, SomeBean> hmap = new HashMap<>();
            LinkedHashMap<SomeBean, SomeBean> linkedhmap = new LinkedHashMap<>();
    
                    long startTime = System.nanoTime();
    
                    for (int i = 0; i < 100000; i++) {
                        SomeBean someBean = new SomeBean(i);
                        linkedhmap.put(someBean, someBean);
                    }
                    long endTime = System.nanoTime();
                    long duration = endTime - startTime;
            System.out.println("LinkedHashMap put: " + duration);
    
                    // Hash add
                    startTime = System.nanoTime();
    
                    for (int i = 0; i < 100000; i++) {
                        SomeBean someBean = new SomeBean(i);
                        hmap.put(someBean, someBean);
                    }
                    endTime = System.nanoTime();
                    duration = endTime - startTime;
            System.out.println("Hashmap put:       " + duration);
    
            linkedhmap.clear();
            hmap.clear();
    
            System.out.println("Clear and re-enter");
    
            startTime = System.nanoTime();
            for (int i = 0; i < 100000; i++) {
                SomeBean someBean = new SomeBean(i);
                linkedhmap.put(someBean, someBean);
            }
            endTime = System.nanoTime();
            duration = endTime - startTime;
            System.out.println("LinkedHashMap put: " + duration);
    
            // hash add
            startTime = System.nanoTime();
    
            for (int i = 0; i < 100000; i++) {
                SomeBean someBean = new SomeBean(i);
                hmap.put(someBean, someBean);
            }
            endTime = System.nanoTime();
            duration = endTime - startTime;
            System.out.println("Hashmap put:       " + duration);
        }
    }
    This outputs:
    PASS 1 ##########
    LinkedHashMap put: 22486976
    Hashmap put: 15263289
    Clear and re-enter
    LinkedHashMap put: 3323120
    Hashmap put: 2939462
    PASS 2 ##########
    LinkedHashMap put: 10133568
    Hashmap put: 12272209
    Clear and re-enter
    LinkedHashMap put: 2323679
    Hashmap put: 2031181


    I did two passes to ensure the JVM was warmed up and had compiled everything it was going to.
    The second pass is, therefore, the key one.

    There's clearly something going on that is taking time. If you reverse it so the HashMap is filled first then the above pre-clearing figures are reversed. Indeed, it could simply be a warming up thing. Timing things, especially relatively small things like this, is not easy since the JVM does lots of stuff under the hood.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: HashMap vs LinkedHashMap

    You can also be pedant about the terminology used: the book mentions iteration to be faster, not value-fetching as is done in the test.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: HashMap vs LinkedHashMap

    Timing things, especially relatively small things like this, is not easy since the JVM does lots of stuff under the hood.

    Ok forget about the code. Do you agree with the author or not and why?

  7. #7
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: HashMap vs LinkedHashMap

    You can also be pedant about the terminology used: the book mentions iteration to be faster, not value-fetching as is done in the test.
    Ok

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: HashMap vs LinkedHashMap

    Well, yes, but the timings (as originally given) did appear to show the linkedhashmap being faster on a put than a straight hashmap, which is not what the SCJP book (or indeed logic) would expect to be the case.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: HashMap vs LinkedHashMap

    Quote Originally Posted by suhaas_mohandos View Post
    Timing things, especially relatively small things like this, is not easy since the JVM does lots of stuff under the hood.

    Ok forget about the code. Do you agree with the author or not and why?
    Look at the code for the two classes on a put.
    The HashMap "put" is the entry point for both, and the LinkedHashMap overrides addEntry, which calls the HashMap addEntry and then does some additional processing to do the linking.
    So clearly the LHM is doing more work.

    No look at how the iteration works. LHM has a set of its own classes for iterating over the entries.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: HashMap vs LinkedHashMap

    The HashMap "put" is the entry point for both, and the LinkedHashMap overrides addEntry, which calls the HashMap addEntry and then does some additional processing to do the linking.

    How come linkedList is faster than arrayList when we use add method on both?

    No look at how the iteration works. LHM has a set of its own classes for iterating over the entries.
    What are you trying to imply?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: HashMap vs LinkedHashMap

    Quote Originally Posted by suhaas_mohandos View Post
    The HashMap "put" is the entry point for both, and the LinkedHashMap overrides addEntry, which calls the HashMap addEntry and then does some additional processing to do the linking.

    How come linkedList is faster than arrayList when we use add method on both?
    Because the LinkedList "add" is not the same as the ArrayList "add".

    There is no shared code there, unlike HashMap and LinkedHashMap.

    Quote Originally Posted by suhaas_mohandos View Post
    No look at how the iteration works. LHM has a set of its own classes for iterating over the entries.
    What are you trying to imply?
    That, unlike the "put" case, the code for iterating between LinkedHashMap and HashMap is very different.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. [SOLVED] LinkedHashMap
    By mtyoung in forum Advanced Java
    Replies: 6
    Last Post: 04-06-2009, 05:20 AM
  5. LinkedHashMap insertion whilst iterating
    By Paul Richards in forum Advanced Java
    Replies: 7
    Last Post: 02-13-2009, 01:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •