How arraylist and hashmap work
Download
1 / 11

How ArrayList and HashMap Work - PowerPoint PPT Presentation


  • 122 Views
  • Uploaded on

How ArrayList and HashMap Work. As you arrive Get the handout Snarf the code for today’s class Go online to Piazza and take the “What should Mike review Wednesday?” poll. After today you will…. Be able to explain how HashMap and ArrayList work

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' How ArrayList and HashMap Work' - aziza


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
How arraylist and hashmap work

How ArrayList and HashMap Work

As you arrive

Get the handout

Snarf the code for today’s class

Go online to Piazza and take the “What should Mike review Wednesday?” poll


After today you will
After today you will…

  • Be able to explain how HashMap and ArrayList work

  • Be able to derive/reason about the runtimes of ArrayList and HashMap

Derive, don’t memorize


  • “There is no doubt that Marley was dead. This must be distinctly understood, or nothing wonderful can come of the story I am going to relate. If we were not perfectly convinced that Hamlet's Father died before the play began, there would be nothing more remarkable in his taking a stroll at night…”


Accessing the nth element of an array is o 1
Accessing the nth element of an array is O(1) distinctly understood, or nothing wonderful can come of the story I am going to relate. If we were not perfectly convinced that Hamlet's Father died before the play began, there would be nothing more remarkable in his taking a stroll at night…”

String var = myArray[1];

//THE SAME SPEED?!?

var = myArray[100000];

//Also the same speed

myArray[100000] = “Ninjas”;

Accessing elements of an array is very fast, but some other parts of arrays are more problematic.

You have to decide on their size up front

You can only index them by consecutive integers


What if we want to expand an array but we have no slots left
What if we want to expand an Array but we have no slots left?

  • Look at ArrayGrow.java

  • Answer questions 1 and 2 in the handout, then go on to 3 if you have time



Now it makes sense arraylist
Now it makes sense: list?ArrayList

  • Add/Remove at (n-k)th element – O(k) because you need to move all the elements one to the right or left

  • Remove at end O(1) – so long as we don’t want to copy to a smaller array

  • Add at end O(1) – sort of. 99% of the time, it is really O(1) because we have empty slot. We normally ignore the other 1%.

  • Get/set kth element O(1) – because it’s an array!


On to hashmap
On to list?hashMap

  • Silly analogy

  • Apartment that assigns rooms based on phone number


Ok on to arraylisthash
Ok, on to list?ArrayListHash

  • Like hashmap

  • Answer questions 4, 4 (numbering mishap), and 5


Now it makes sense hashmap
Now it makes sense: list?HashMap

  • Get an element based on key O(1): Just a lookup in an array

  • Check if a key exists: Just a lookup in the array

  • Set an element based on key O(1)*: another lookup, and we won’t worry about the situation in which we need to grow the hashmap

    Note that this only works if the hashCode function is good


After today you will1
After today you will… list?

  • Be able to explain how HashMap and ArrayList work

  • Be able to derive/reason about the runtimes of ArrayList and HashMap

Derive, don’t memorize


ad