What is the difference between a set and a map? The union of two sets A and B is
ID: 3555690 • Letter: W
Question
What is the difference between a set and a map?
The union of two sets A and B is the set of all elements that are contained in A, B, or both. The intersection is the set of all elements that are contained in A and B. How can you compute the union and intersection of two sets, using the add and contains methods, together with an iterator?
How can you compute the union and intersection of two sets, using some of the methods that the java.util.Set interface provides, but without using an iterator? (Look up the interface in the API documentation.)
Can a map have two keys with the same value? Two values with the same key? A map can be implemented as a set of (key, value) pairs. Explain. Verify the hash code of the string "Juliet" in Table 6. Verify that the strings "VII" and "Ugh" have the same hash code.
Consider the algorithm for traversing a maze from Section 15.6.4 Assume that we start at position A and push in the order West, South, East, and North. In which order will the lettered locations of the sample maze be visited?
Explanation / Answer
lists are ordered, and you can have duplicates.
i.e. you add A B C A D, and if you iterate through, you'll get A B C A D right back.
Sets are unordered, and you can't have duplicates.
So if you add A B C A D, you could get back A B C D or A C B D etc.
Maps are key-value pairs, rather than just a collection of items. You can have duplicate values, but not duplicate keys.
So you add in things like:
name=fred
age=22
dogsname=fred
To iterate through them you'll have to say map.keyset.iterator, then use the keys to lookup the values. There's also something like map.valueset or .entryset if you want to go through the values without knowing the keys.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.