1 / 11

# How ArrayList and HashMap Work - PowerPoint PPT Presentation

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

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

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

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

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

• 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) 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

• 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: 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 list?hashMap

• Silly analogy

• Apartment that assigns rooms based on phone number

Ok, on to list?ArrayListHash

• Like hashmap

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

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

• 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