chapter 12- sets

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How many elements are in the set { 83, 5 } ∩ { 9, 77, 83 }?

1-- 83 is the only element that exists in both sets and will be the only element in the intersection

How many elements are in a set that is built by adding the element 28 6 times, then the element 54 9 times? 1 2 15

2- The set is { 28, 54 }

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> playerScores = new HashMap<String, Integer>(); playerScores.put("Jake", 14); playerScores.put("Pat", 26); playerScores.put("Hachin", 60); playerScores.put("Michiko", 21); playerScores.put("Pat", 31); What value will playerScores.get("Pat") return

31 Only one values is associated for each key. The statement playerScores.put("Pat", 31) replaces the previously associated value (26) with the value 31

How many elements are in the set { 83, 5 } ∪ { 9, 77, 83 }?

4-- { 83, 5 } ∪ { 9, 77, 83 } = { 83, 5, 9, 77 }, which has 4 elements.

{ 83, 5 } \ { 9, 77, 83 } = ?

5-- 5 is the only element that exists in { 83, 5 } but not in { 9, 77, 83 }.

HashMap and TreeMap are ADTs implementing the Map interface. Although both HashMap and TreeMap implement a Map, a programmer should select the implementation that is appropriate for the intended task.

A HashMap typically provides faster access but does not guarantee any ordering of the keys, whereas a TreeMap maintains the ordering of keys but with slightly slower access. This material uses the HashMap class, but the examples above can be modified to use TreeMap

A filter operation on set X produces a subset containing only elements from X that satisfy a particular condition. The condition for filtering is commonly represented by a filter predicate:

A function that takes an element as an argument and returns a Boolean value indicating whether or not that element will be in the filtered subset.

Set elements may be primitive data values, such as numbers or strings, or objects with numerous data members. When storing objects, set implementations commonly distinguish elements based on an element's key value:

A primitive data value that serves as a unique identifier for the element. Ex: An object for a student at a university may store information such as name, phone number, and ID number. No two students will have the same ID number, so the ID number can be used as the student object's key.

If the map does not contain the specified key, the get() method returns null.

A programmer can use the containsKey() method to check if a map contains the specific key. In the program above, statePopulation.containsKey("NY") would return false.

Given a key, a set search operation returns the set element with the specified key, or null if no such element exists. The search operation can be used to implement a subset test.

A set X is a subset of set Y only if every element of X is also an element of Y.

Which of the following is not a valid set? { 78, 32, 46, 57, 82 } { 34, 8, 92 } { 78, 28, 91, 28, 15 }

A set does not contain duplicates. { 78, 28, 91, 28, 15 } contains 28 twice and is therefore not a valid set.

Different students at the same university may have the same name or phone number, but each student has a unique ID number.

A set for the course roster uses the student ID as the key value, since the exact same student cannot enroll twice in the same course. The call to remove Student C provides only the student ID.

Which is true for any set X? X ∪ X = X ∩ X X ∪ X = X \ X X \ X = X ∩ X

A-- The union of X and itself is X, and the intersection of X and itself is also X.

To test if set2 is a subset of set1, each element of set 2 is searched for in set1. Elements 19, 22, and 26 are found in set1. Element 34 is in set2 but not set1, so set2 is not a subset of set1. The first element in set3, 88, is not in set1, so set3 is not a subset of set1.

All elements of set4 are in set1, so set4 is a subset of set1. No other set is a subset of another. But each set is always a subset of itself.

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> exMap = new HashMap<String, Integer>(); exMap.put("Tom", 14); exMap.put("John", 26); exMap.put("Mary", 13); exMap.put("Hans", 90); exMap.put("Franz", 88); exMap.clear()

All entries are removed from the map

Which 2 sets are equivalent? { 56, 19, 71 } and { 19, 65, 71, 56 } { 88, 54, 81 } and { 81, 88, 54 } { 39, 56, 14, 11 } and { 14, 56, 93, 11 }

B- Both sets contain elements 54, 81, and 88.

When X and Y do not have any elements in common, which is always true? X ∪ Y = X ∩ Y X ∩ Y = X \ Y X \ Y = X

C--- Because Y does not contain any elements that also exist in X, X \ Y is equivalent to X.

{ 9, 77, 83 } \ { 83, 5 } = ?

Correct 9, 77---Elements 9 and 77 exist in { 9, 77, 83 }, but not in { 83, 5 }, and therefore make up the difference set.

A dynamic set is a set that can change after being constructed. A static set is a set that doesn't change after being constructed. A collection of elements is commonly provided during construction of a static set, each of which is added to the set

Ex: A static set constructed from the list of integers (19, 67, 77, 67, 59, 19) would be { 19, 67, 77, 59 }.

A map operation on set X produces a new set by applying some function F to each element.

Ex: If X = {18, 44, 38, 6} and F is a function that divides a value by 2, then SetMap(X, F) = { 9, 22, 19, 3 }

A set is a collection of distinct elements. A set add operation adds an element to the set, provided an equal element doesn't already exist in the set. A set is an unordered collection.

Ex: The set with integers 3, 7, and 9 is equivalent to the set with integers 9, 3 and 7.

The intersection of sets X and Y, denoted as X ∩ Y, is a set that contains every element that is in both X and Y, and no additional elements.

Ex: { 54, 19, 75 } ∩ { 75, 12 } = { 75 }

The union of sets X and Y, denoted as X ∪ Y, is a set that contains every element from X, every element from Y, and no additional elements.

Ex: { 54, 19, 75 } ∪ { 75, 12 } = { 12, 19, 54, 75 }

The Set interface defined within the Java Collections Framework defines a Collection of unique elements. The Set interface supports methods for adding and removing elements, as well as querying if a set contains an element.

For example, a programmer may use a set to store employee names and use that set to determine which customers are eligible for employee discounts.

The HashMap type is an ADT implemented as a generic class (discussed elsewhere) that supports different types of keys and values.

Generically, a HashMap can be declared and created as HashMap<K, V> hashMap = new HashMap<K, V>(); where K represents the HashMap's key type and V represents the HashMap's value type.

Sets are commonly implemented to use keys for all element types. When storing objects, the set retrieves an object's key via an external function or predetermined knowledge of which object property is the key value. When storing primitive data values, each primitive data value's key is itself.

Given a key, a set remove operation removes the element with the specified key from the set.

Suppose a dynamic set has N elements. Adding any element X and then removing element X will always result in the set still having N elements.

If X were already in the set prior, then the addition would not increase the number of elements. The subsequent removal would then decrease the number of elements to N - 1.

The add() method does not add duplicate elements to a set.

If a programmer tries to add a duplicate element, the add() method fails and returns false. Otherwise, add() returns true.

The remove() method only removes elements that exist within the set.

If the element exists, the remove() method removes the element and returns true. Otherwise, remove() returns false.

Adding 67, 91, and 14 produces a set with 3 elements. Because 91 already exists in the set, adding 91 any number of additional times has no effect

Set 2 is built by inserting the same numbers in a different order. Because order does not matter in a set, the 2 sets are equivalent.

Which set operation is not commutative?

Set difference is not a commutative operation; Set intersection and union are a commutative operation.

The union and intersection operations are commutative, so X ∪ Y = Y ∪ X and X ∩ Y = Y ∩ X.

The difference operation is not commutative

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> exMap = new HashMap<String, Integer>(); exMap.put("Tom", 14); exMap.put("John", 26); exMap.put("Mary", 13); exMap.put("Hans", 90); exMap.put("Franz", 88); exMap.remove("Tom")

The entry mapping the key "Tom" to the value 14 is removed from the map

The put() method associates a key with the specified value.

The get() method returns the value associated with a key

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> exMap = new HashMap<String, Integer>(); exMap.put("Tom", 14); exMap.put("John", 26); exMap.put("Mary", 13); exMap.put("Hans", 90); exMap.put("Franz", 88); exMap.putIfAbsent("Frederique", 36);

The key "Frederique" does not exist. So a new entry is added mapping "Frederique" to the value 36.

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> exMap = new HashMap<String, Integer>(); exMap.put("Tom", 14); exMap.put("John", 26); exMap.put("Mary", 13); exMap.put("Hans", 90); exMap.put("Franz", 88); exMap.putIfAbsent("Mary", 17);

The key "Mary" already exists, so the value for the key is not updated.

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> exMap = new HashMap<String, Integer>(); exMap.put("Tom", 14); exMap.put("John", 26); exMap.put("Mary", 13); exMap.put("Hans", 90); exMap.put("Franz", 88); exMap.remove("john");

The key "john" is different from the key "John". So, no entries are removed from the map

The add() method adds an item such as a String to a set. The contains() method returns true if an item is in the set, and otherwise returns false.

The remove() method removes an item from a set.

A set does not contain duplicate items. The add() method adds an item only if the item does not already exist. add() returns true if item added, else returns false.

The remove() method returns true if item removal was successful, otherwise returns false

A programmer may wish to lookup values or elements based on another value, such as looking up an employee's record based on an employee ID. The Map interface within the Java Collections Framework defines a Collection that associates (or maps) keys to values.

The statement import java.util.HashMap; enables use of a HashMap.

The HashSet type is an ADT implemented as a generic class that supports different types of elements. A HashSet can be declared and created as HashSet<T> hashSet = new HashSet<T>(); where T represents the HashSet's type, such as Integer or String.

The statement import java.util.HashSet; enables use of a HashSet within a program.

The put() method associates a key with the specified value. If the key does not already exist, a new entry within the map is created. If the key already exists, the old value for the key is replaced with the new value.

Thus, a map associates at most one value for a key. The get() method returns the value associated with a key, such as statePopulation.get("CA").

add

add(element) If element does not exist, adds element to the set and returns true. If element already exists, returns false.

clear

clear() Removes all map entries.

contains

contains(element)Returns true if element exists, otherwise returns false.

containsKey

containsKey(key) Returns true if key exists, otherwise returns false.

containsValue

containsValue(value) Returns true if at least one key is associated with the specified value, otherwise returns false.

select whether a program should use a static or dynamic set. List of contacts for a user

dynamic-- To allow the user to add or remove contacts from their contact list, a dynamic set must be used.

Given the following code that creates and initializes a HashSet: HashSet<Integer> employeeIDs = new HashSet<Integer>(); employeeIDs.add(1001); employeeIDs.add(1002); employeeIDs.add(1003); Write a statement that adds an employee ID 1337

employeeIDs.add(1337); Adds the value 1337 to the set.

Given the following code that creates and initializes a HashSet: HashSet<Integer> employeeIDs = new HashSet<Integer>(); employeeIDs.add(1001); employeeIDs.add(1002); employeeIDs.add(1003); Write a statement that removes the employee ID 1002.

employeeIDs.remove(1002); Removes the value 1002 from the set.

Given the following code that creates and initializes a HashSet: HashSet<Character> guessedLetters = new HashSet<Character>(); guessedLetters.add('e'); guessedLetters.add('t'); guessedLetters.add('a'); What value will guessedLetters.add('e') return?

false add() returns false because 'e' already exists in the set.

Given the following code that creates and initializes a HashSet: HashSet<Character> guessedLetters = new HashSet<Character>(); guessedLetters.add('e'); guessedLetters.add('t'); guessedLetters.add('a'); What value will guessedLetters.remove('s') return?

false remove() returns false because 's' is not in the set.

Calling SetMap on set X always produces a set with the same number of elements as X.

false- Although SetMap adds X items to the result set, the map function may produce the same result for different elements. Ex: When mapping the set { 3.6, 3.7 } with a function that rounds numbers to the nearest integer, the number 4.0 is added to the result set twice. The second addition has no effect, and the resulting set is { 4.0 }.

The loop in SetIsSubset always performs N iterations, where N is the number of elements in subsetCandidate

false- As soon as an element in subsetCandidate is found to be missing from set, false is returned, even if many elements from subsetCandidate have not yet been checked

SetRemove(courseRosterSet, "Student D") would remove Student D from the set.

false- Calls to SetRemove must use the key value, which is the student ID, not the student name

Calling SetFilter on set X always produces a set with the same number of elements as X.

false- SetFilter doesn't include any elements that do not satisfy the predicate, so the resulting set may have fewer than X elements.

SetRemove will not operate properly on an empty set

false- SetRemove would not find any elements in the set with a matching key, regardless of the key argument's value. Therefore, element would be null and no action would be taken, which is the proper behavior for trying to remove from an empty set.

If the student objects contained a field for GPA, then GPA could be used as the key value instead of student ID.

false- Two or more different students are likely to have the same GPA. A key value must be a unique identifier

Static sets do not support union or intersection, since these operations require changing the set.

false-- Union and intersection can be implemented to build and return a new set, without modifying either set involved in the union.

A static set constructed from the list of integers (20, 12, 87, 12) would be { 20, 12, 87, 12 }

false-- { 20, 12, 87, 12 } is not a valid set because the element 12 exists twice. A static set may be constructed from a list containing duplicate values, but the constructed set would not contain duplicates.

get

get(key)Returns the value associated with key. If key does not exist, return null.

Given the following code that creates and initializes a HashSet: HashSet<Character> guessedLetters = new HashSet<Character>(); guessedLetters.add('e'); guessedLetters.add('t'); guessedLetters.add('a'); Write a single statement that adds to guessedLetters the variable favoriteLetter only if favoriteLetter does not already exist in the set

guessedLetters.add(favoriteLetter); add() adds an element to a set only if the element is not already in the set. The return value indicates if the method successfully added the element.

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> playerScores = new HashMap<String, Integer>(); playerScores.put("Jake", 14); playerScores.put("Pat", 26); playerScores.put("Hachin", 60); playerScores.put("Michiko", 21); playerScores.put("Pat", 31); Write a statement to assign Hachin's score to a variable named highScore.

highScore = playerScores.get("Hachin"); get() method returns the current value associated with the key. For the key "Hachin", the value 60 is returned and highScore is assigned with that value.

keySet

keySet() Returns a Set containing all keys within the map.

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> playerScores = new HashMap<String, Integer>(); playerScores.put("Jake", 14); playerScores.put("Pat", 26); playerScores.put("Hachin", 60); playerScores.put("Michiko", 21); playerScores.put("Pat", 31); Write a single statement to update Jake's score to the value 34

playerScores.put("Jake", 34); Replaces the existing value associated with the key "Jake" with the new value 34

Given the following code that creates and initializes a HashMap: HashMap<String, Integer> playerScores = new HashMap<String, Integer>(); playerScores.put("Jake", 14); playerScores.put("Pat", 26); playerScores.put("Hachin", 60); playerScores.put("Michiko", 21); playerScores.put("Pat", 31); Write a statement to add a mapping for a player named Kira with a score of 1.

playerScores.put("Kira", 1); Associates the key "Kira" with the value 1

put()

put(key, value) Associates key with specified value. If key already exists, replaces previous value with specified value.

putIfAbsent

putIfAbsent(key, value) Associates key with specified value if the key does not already exist or is mapped to null

remove

remove(element) If element exists, removes element from the set and returns true. If the element does not exist, returns false.

remove

remove(key) Removes the map entry for the specified key if the key exists.

size

size() Returns the number of elements in the set

select whether a program should use a static or dynamic set. Collection of names of all countries on the planet

static-- All country names can be provided at construction. Country names should not be added or removed after construction because the country names will not change while the program executes.

select whether a program should use a static or dynamic set. Periodic table of elements

static-- All elements can be provided at construction. Elements should not be added or removed after construction because the contents of the periodic table will not change while the program executes

Given the following code that creates and initializes a HashSet: HashSet<Integer> employeeIDs = new HashSet<Integer>(); employeeIDs.add(1001); employeeIDs.add(1002); employeeIDs.add(1003); What value will employeeIDs.contains(1001) return?

true The ID 1001 exists in the set, so the statement returns true.

Every set is a subset of itself.

true- A set will contain every element of itself, and therefore is a subset of itself.

For X to be a subset of Y, the number of elements in Y must be greater than or equal to the number of elements in X.

true- For all elements in set X to also be in set Y, Y must have at least the same number of elements in X.

A filter predicate must return true for elements that are to be added to the resulting set, and false for elements that are not to be added

true- The filter predicate, by definition, has this behavior.

Both SetFilter and SetMap will call the function passed as the second argument for every element in the set.

true-- Every element is tested with the predicate when filtering, and every element transformed with the map function when mapping.

values

values() Returns a Collection containing all values within the map

The difference of sets X and Y, denoted as X \ Y, is a set that contains every element that is in X but not in Y, and no additional elements.

{ 54, 19, 75 } \ { 75, 12 } = { 54, 19 }


Set pelajaran terkait

Chapter 4: Cell Biology, Signal Transduction, and the Molecular Biology of Cancer

View Set

AP Exam Multiple Choice questions 1-45

View Set

VSim Next-Gen Maternity Case 5: Fatime Sanogo

View Set

6-2 Worksheet-Internal components of the X-ray Tube Anode

View Set

(HA Ch 3) PrepU - Interviewing and Communication

View Set

international business ch 17 module

View Set