2.4 - Map and Hashmap

Ace your homework & exams now with Quizwiz!

What's the name for the type of search that's performed when you search through a List or ArrayList for something? Is it fast or slow? What's the time complexity?

A Linear Search. The time complexity is O(n), meaning they're linear time complexity, which is slow because as the list grows, so will the time it takes to search it.

What are the rules around duplicates in a Map?

A Map cannot contain duplicate keys. However it can contain duplicate Values. You can have two non-identical Keys both refer to an identical Value.

What is a Map?

A Map is a data structure that stores key and value pairs.

What is a key and what is a value?

A key is similar to an ArrayList's index and the value is the object at the index.

Misc: What's another name for when you call the "default" constructor for an Object type in Java?

Calling the no-args constructor for the object

What methods does the get() method utilize?

First it utilizes the hashCode() method for the Key object that we passed in as an argument. Then, if something is already in that index in Map, it uses the argument's equals() method to compare to other items in the List

What if we have two Keys that are Strings "int" and "map", and "map" is already in our Map, and we want to add "int", but what if they both have the same HashCode value, what will happen?

First, the put() method will run String's hashCode() method to get the HashCode for Map. Then, it will go to that point in the HashMap. It will see that there is already a value in this slot (remember, each "slot" in a Map actually holds a List in order to handle collisions). Since there's already something in this slot, the put() method will call String's equals() method, to compare "map" to "int". It will return FALSE, since they're not the same, and the put() method will insert "int" into the List at this slot in the HashMap. Finally, the put() method will return null

What are the three commonly used Map implementations?

HashMap TreeMap LinkedHashMap

What if we want to store more than one Value for each Key? How could we accomplish this? (imagine our Values are String)

Instead of passing String as the variable type for our Value, we could pass List<String>. Each Key would have a corresponding List which would hold Strings

What data type is the Key in a Map?

It can be any object. In our example of a Dictionary, the term could be the Key and the definition could be the Value.

Will hashCode() always return the same int for the same object, or does it sometimes return different values for the same object?

It should ALWAYS return the same int value for the same object.

What is the relationship between Map and Entry<Key, Value>?

It's a nested Interface relationship. Entry<Key, Value> is an Interface nested inside of the Map interface. This is a rare relationship and is only used when the two are tightly related, and is usually only useful to the enclosing class.

What are three examples of good times to use the Map data structure?

Lookup: Dictionary was a good example. Another is using an attribute of an object as a Key, and the Object itself as the Value. (customerId as the Key and Customer object as the Value) Aggregation: Example, you want to count how many of each word in a document. You could use the words as the Keys, and the count of them as the Value. As you go through the document, you increment the Value of each word as you move through it. Building per-key lists: Basically when you're storing Lists for a Key.

What kind of thing is Map in Java?

Map is an Interface. That means we cannot instantiate a Map. Instead, we use a class that implements Map.

What does the code look like to declare a HashMap? What is this similar to?

Map<Key, Value> hashMap = new HashMap<>(); This is like declaring a reference variable with the List interface, then instantiating an ArrayList object type. In this case, our reference type is Map, and our object type is HashMap.

How would the code look in our Dictionary example specifically?

Map<String, String> hashMap new HashMap<>(); If you remember, in our example we're storing terms and definitions, and both are String objects. The term is the Key, and its definition is the Value.

Can Maps accept primitive values as an argument data type? What can Maps except as data types for Key and Value?

No, they cannot except primitives. If you wanted to use int, you would have to use Integer instead. Maps can accept any Java Object as a parameter data type, but only Objects.

Does the order in which you add Objects to a Map effect the order in which they're stored?

No. That's how a List works. Items in a Map are stored based on the key's hash code value.

If you make a call to retrieve all entries in a Map two times consecutively, will they be returned in the same order both times?

No. They may return entries in a different order. I don't really understand this and I'm not sure whether or not it's true. This would be a good thing to ask in office hours. Could search for some of these keywords in that reading to find the specific sentence. Because if they're stored by their hashcodes, and the hashcodes are the same each time they're generated (which they have to be) then I'd think these would come out in the same order when called two times consecutively

What's the time complexity in retrieving something from a GIVEN spot in a List or Array? Why?

O(1). Because if it's a GIVEN spot, we know where it is, and we just have to go find it and return it, we don't have to iterate over the list checking each item to see if it's the right item.

What's an important pre-requisite for using Maps in software development?

That we have a strong key:value association. Our Dictionary example is a great example of that.

What does the put method do? What does it return? <-- IMPORTANT

The put method places a Key:Value pair into a Map. If the Key already exists, the Value is replaced with the Value passed in. The method will return whatever Value WAS there for the given Key (if it was found to already be there and replaced). Alternatively it will return null if the Key wasn't in the Map.

What method do we use to remove things from a Map? How does it work?

We use remove() It removes BOTH the Key and the Value for the Key that we pass in. It returns the Value that was removed (if one was found) It returns null if a matching Key was not found.

How do we iterate over an entire HashMap? What would the code look like to print out our Dictionary example? What does this syntax mean at a high level?

We use the entrySet() method here to iterate over the HashMap. The entrySet() method returns a Set of Map.Entry<Key, Value> objects. Key and Value are the same types that are defined by the Map that the entrySet() method was called on. getKey() and getValue() are two methods in the Entry<Key, Value> interface.

What function do we use to return the Value for a specific Key in a Map? How is it written in code?

We use the get() method!

Given these two lines of code below, if afterwards we call all the Values for "loop" in "dictionaryMap", will this additional definition be returned as well? List<String> loopDefinitions = dictionaryMap.get("loop"); loopDefinitions.add("Repeats execution of code for each item in a collection (for-each).");

Yes it will. The reason is because when we declare loopDefinitions and instantiate it with dictionaryMap.get("loop"), we're creating a shallow copy. This means that loopDefinitions is simply a reference to the List<String> stored in the Map. In this case that's good, because it makes it easier for us to make adjustments to that list. Cool!

What method do we use if we just need to check if a Key is in a Map? (Not get the Value stored for that Key) Does it operate basically the same as get()?

containsKey() Yes

What does the get() method return if nothing matches the Key that we pass in as a parameter?

null


Related study sets

Vertical Milling / Cutting Tools

View Set

A&P - Joint Structural/Functional Classifications

View Set

Remediation Assignment for Exam 1

View Set

Eg101-Otto and Diesel Cycles quiz 4

View Set

Chapter 40: Management of Patients with Gastric and Duodenal Disorders

View Set

Earth Science--Weathering, Erosion, and Deposition--8th Grade

View Set

Chapter 18 and 19 Questions Review

View Set

AP Human Geography Chapter 1 Study Guide

View Set

Section 1: Origination and Underwriting

View Set