Level 132 Level 134
Level 133

[Quiz] Collections II


112 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Ignore?
What can static methods reference?
A static method can reference static variables or local variables
What's a more efficient way to test code than a test driver?
Including the main method in the class with the code to be tested
Add an Element to the collection
Name the basic operations supported by all collections
What is an ADT?
Abstract data type is an organized collection of information and a set of operations used to manage that information
Fast
Guaranteed to run in O(n log n) time.
Stable
It doesn't re-order equal elements (i.e. sorting email inbox by date and then sender means that the contiguous messages from a sender will still be sorted by date if the second sort was stable).
Map.Entry<K,V> pollFirstEntry()
NavigableMap<String, String> tailmap = mymap.tailMap("bb", false); //LINE 3
What is the requirement for BinarySearch to work in Collections and Arrays?
The binarySearch() method works ONLY if the collection or array being searched is already sorted. Further, the sort must have been done using the same Comparator that is being used to search otherwise search res…
boolean equals(Object obj)
What are some methods in the "Object" class that are available to all classes inherited from it?
What are the terms of the Object's equals() method contract?
Reflexive. Ex: for a given x, x.equals(x) should be true
public int hashCode() { return x*17; }
Both A and B are valid hashcode implementations
x.equals(y) == true
A is Required, but B is NOT Required.
Map Implementations:
What are the key Concrete "Map" Collections Implementations in Java?
Set Implementations:
What are the key Concrete "Set" Collections Implementations in Java?
List Implementations:
What are the key Concrete "List" Collections Implementations in Java?
What is the difference between Collections and Collection?
Collection (capital C), which is actually the java.util.Collection interface from which Set, List, and Queue extend. (That's right, extend, not implement. There are no direct implementations of Collection.)
Answer is:
if (a.equals(b)) System.out.println("equal");
List<> stuff = new ArrayList<String>();
A and B are correct
And what is the output of that method?
The Comparable interface is used by the Collections.sort() method and the java.util.Arrays.sort() method to sort Lists and arrays of objects, respectively. To implement Comparable, a class must implement a single method, compareTo().
Classes implementing Comparator interface should implement what method?
compare() method should be implemented by classes implementing Comparator interface.
Arrays.asList()
How do you convert Arrays to List and List to Arrays?
Output is:
System.out.println(map + " | " + submap);
Both are correct syntaxes.
public void addAnimal(List<? extends Animal> animals) { }
public void addData( List<?> foo ) { }
the wildcard <?> means, simply "any" type. So, the it could be of type <Dog>, <Integer>, <String>, etc.
Answer: B
H. An exception is thrown at runtime
public. Obj 6.2.
What is the access modifier for equals(), hashCode() and toString()?
Why should you override toString()?
So that methods like System.out.println() will produces something readable, like your object's state. Obj 6.2.
Why should you use "==" with reference variables?
To determine if the reference variables refer to the same object. Obj 6.2.
Why should you use equals()?
To determine if two objects are meaningfully equivalent. Obj 6.2.
How does overriding equals() affect an object's utility as a hash key?
If an object doesn't override equals(), it won't be useful as a hash key. Obj 6.2.
How does overriding equals() affect different objects' ability to be considered equal?
If an object doesn't override equals(), different instances will NOT be considered equal. Obj 6.2.
Some good examples are String and the wrapper classes (Integer, Long, etc.). Obj 6.2.
What are some examples of objects that already override equals() and therefore make good hash keys?
What operator should be part of every equals() method override?
The instanceof operator should be used to be sure you're evaluating an appropriate class. Obj 6.2.
All of the objects' significant attributes. Obj 6.2.
What should be compared when overriding equals() in an object?
Reflexive: x.equals(x) is always true.
What 5 attributes define the equals() contract?
x.hashCode() == y.hashCode() is true. Obj 6.2.
If x.equals(y) is true, what is the relationship between the responses from hashCode() for x and y?
hashcode(). Obj 6.2.
If you are overriding equals(), what else should you override?
What are 5 collections in the Java API that use hashing?
HashMap, HashSet, Hashtable, LinkedHashMap & LinkedHashSet all use hashing. Obj 6.2.
Satisfies the hashCode() contract. Obj 6.2.
In general, what does an appropriate hashCode() override do?
What makes a hashCode() override efficient?
It's ability to distribute keys evenly across its "buckets". Obj 6.2.
At least as precise as its sibling hashCode() method. Obj 6.2.
At the very least, how precise must an overridden equals() method be?
Their hashcodes must be equal. Obj 6.2.
If two objects are equal what does that mean for their hashcodes?
It is legal, but very inefficient. Obj 6.2.
What is the legality and or efficiency of a hashCode() method that returns the same value for all instances?
What are 3 components of the hashCode() contract?
Consistency: multiple calls to x.hashCode() return the same integer (assuming x is not changed).
How should transient variables be handled in equals() and hashCode() methods?
They should not be a part of either method. Obj 6.2.
What are 5 common collection activities?
Adding objects, removing objects, verifying object inclusion, retrieving objects, and iterating. Obj 6.1.
collection: Represents the data structure in which objects are stored.
What are the three meanings for "collection" when dealing with collections in Java?
Lists, Sets, Maps, and Queues. Obj 6.1.
What are 4 basic collection flavors in Java?
What are the general attributes of a List?
It is ordered, allowed to contain duplicates and has an index. Obj 6.1.
What are the general attributes of a Set?
It may or may not be ordered and/or sorted; and duplicates are not allowed. Obj 6.1.
What are the general attributes of a Map?
It may or may not be ordered and/or sorted; and duplicate KEYS are not allowed. Obj 6.1.
What are the general attributes of a Queue?
It is ordered by FIFO or by priority. Obj 6.1.
Sorted, Unsorted, Ordered and Unordered. Obj 6.1.
What are 4 basic collection sub-flavors in Java?
In a specific non-random order. Obj 6.1.
How does iteration work with an Ordered collection?
In a sorted order. Obj 6.1.
How does iteration work with a Sorted collection?
What are 3 categories of sorting?
Alphabetic, numeric and programmer-defined. Obj 6.1.
What are key attributes of ArrayList?
Fast iteration and fast random access. Obj 6.1.
What are key attributes of Vector?
Like a slower ArrayList, but methods are synchronized. Obj 6.1.
What are key attributes of LinkedList?
Good for adding elements to the ends, i.e., stacks and queues. Obj 6.1.
What are key attributes of HashSet?
Fast access, assures no duplicates, provides no ordering. Obj 6.1.
What are key attributes of LinkedHashSet?
No duplicates, iterates by insertion order. Obj 6.1.
What are key attributes of TreeSet?
No duplicates, iterates in sorted order. Obj 6.1.
What are key attributes of HashMap?
Fastest updates (key/values); allows one null key, many null values. Obj 6.1.
What are key attributes of Hashtable?
Like a slower HashMap due to its synchronized methoeds. No null values or null keys allowed. Obj 6.1.
What are key attributes of LinkedHashMap?
Faster iterations; iterates by insertion order or last accessed; allows one null key, many null values. Obj 6.1.
A sorted map. Obj 6.1.
What are key attributes of TreeMap?
What are key attributes of PriorityQueue?
A to-do list ordered by the elements' priority. Obj 6.1.
What can be stored in a Collection?
Only Objects, but primitives can be autoboxed. Obj 6.3.
What are the two most common ways of iterating over a Collection?
Enhanced for loop or with an Iterator using hasNext() and next(). Obj 6.3.
How does hasNext() affect an Iterator?
It determines if more elements exist, but the Iterator does NOT move. Obj 6.3.
How does next() affect an Iterator?
It returns the next element AND moves the Iterator forward. Obj 6.3.
What must be true of a Map's keys?
They must be an object that overrides equals() and hashCode(). Obj 6.3.
offer() adds an element
What operations are available for Queues?
Two are floor() and higher(). Obj 6.3.
What new navigation methods were added to TreeSets and TreeMaps?
TreeSets and TreeMaps. Obj 6.3.
Of which collections can you create / extend "backed" sub-copies?
What types of sorts can be done on arrays and lists?
Natural order, use a Comparable, or use one or more Comparators. Obj 6.5.
How does implementing Comparable happen?
Mark your class as implementing it and provide a compareTo() method. Obj 6.5.
Just one. Obj 6.5.
How many sort orders does a Comparable implementation provide?
How does implementing Comparator happen?
Mark your class as implementing it and provide a compare() method. Obj 6.5.
They must be comparable. Obj 6.5.
What must be true of a List's elements to be sorted and searched?
It must have been sorted already. Obj 6.5.
What must be true of a List or array to be searched?
What two sort & search methods are provided by both java.util.Collections and java.util.Arrays?
Both provide sort(), which uses a Comparator or natural order, and binarySearch(), which searches a pre-sort()-ed array or List. Obj 6.5.
Arrays.asList(). Obj 6.5.
What method creates a List from an array and links them?
Collections.reverse(). Obj 6.5.
What method reverses the order of elements in a List?
Collections.reverseOrder(). Obj 6.5.
What method provides a Comparator that sorts in reverse of the natural order?
Lists and Sets. Obj 6.5.
What collections support the toArray() method to create arrays?
What is the benefit of Generics?
They let you enforce compile-time type safety on classes and methods declared using generic type parameters. Typically Collections. Obj 6.4.
What type of references can an ArrayList<Animal> accept?
Animals, but also any sub-type of Animal (subclass or implementation). Obj 6.4.
What is the difference between getting an element out of a generic collection vs. non-generic?
Non-generic collections just hold Objects so you must cast any element (that is not an Object) to whatever type you are expecting. Generic collections check that for you at compile time, so no cast is necessary. Obj 6.4.
What could go wrong if you pass a generic collection into a method that takes a non-generic collection?
The method could insert the wrong type into the collection. Since the method expects a non-generic collection, the compiler will not stop this. When you try to take this item out, there will be an exception thrown. Obj 6.4.
A compilation warning. Obj 6.4.
If a generic collection is manipulated in an area where it is being treated as a non-generic collection (for example passing an ArrayList<String> to a method that expects a non-generic ArrayList and adding something to that list), what happens?
How does the phrase, "Compiles without error," relate to compilation warnings?
They are not directly related. Compiling with warnings and no errors is, in fact, compiling without errors. Obj 6.4.
What are all the types of generic information available at run-time?
There is no generic type information available at run-time. This is because of type erasure, after type checking during compilation the actual genericised objects just become non-generic objects. Obj 6.4.
How are polymorphic assignments affected when generics are involved?
Only the base type is allowed assignment in this way. The generic type parameter MUST match! So:
Yes.
Do you have to import the ArrayList class to use it?
Like this:
How can wildcard syntax be used to allow a method to take a List of Dogs or sub-types of Dog?
Like this;
Using wildcard syntax, how do you represent a List of Interfaces called Edible anything that implements Edible?
You can't. These lists are access only, no modifications. Obj 6.4.
How can you modify items in a wildcard specified generic List like List<? extends Dog>?
What are the declaration conventions for generics?
T for type and E for elements, but these are just conventions any value will work. Obj 6.4.
Class, method and variable definitions.
What types of declarations are allowed to utilize generics?
Just use different generic declaration placeholders like:
How does it look to define more than one generic type in a definition?
Each Object will have a unique hash key and be allocated to its own bucket. Obj 6.2
If hashCode() is not overridden for an Object used as a hash key what will happen to allocation in the collection?
How do add() and offer() affect naturally sorted queues?
They both add an element to the natural sort position in the queue. Obj 6.1
How do remove() and poll affect queues?
remove() looks for a passed object in the queue and removes it, returning false if the object is not found; poll() returns the first object in the queue and removes it. Obj 6.1
When does adding disparate types get you into trouble for non-generic collection?
At run-time if your collection is sorted. Otherwise, all is well (until you abuse casting on removal). Obj 6.1
What does TreeMap's higherKey(key) method return?
The key following, but not equal to the given key. Obj 6.3
What does TreeMap's ceilingKey(key) method return?
The key following or equal to the given key. Obj 6.3
What does TreeMap's lowerKey(key) method return?
The key preceding, but not equal to the given key. Obj 6.3
What does TreeMap's floorKey(key) method return?
The key preceding or equal to the given key. Obj 6.3
No. You can't say:
Do polymorphic assignments apply to generic type parameters?
At run-time when you try to insert one of the objects.
If a treeMap contains objects that don't implement Comparable when will you find out about it?
You will not find what you are looking for, but will probably get -1 back.
What happens if you binarySearch() an array or list with no comparator that is sorted with a comparator?