Intermediate Java
Week 5 Review Questions
- 1. What can you do using TreeSet?
-
Keeps elements in a collection sorted and does not allow duplicates.
- 2. What does Collections.sort() method do?
-
Sorts items in an arrayList in ascending order. Used with generics, and overloaded with a comparator class as argument, it allows you to decide by what parameter you want the collection of objects to be sorted.
- 3. What is generics?
-
Generics is a feature added to Java 5 and later which allows you to specify what type of element is going into your Collections, and allows these elements to maintain their element-type-ness within the Collection. With generics, your Collection is "imprinted", so to speak, when you create it, with the type ( or gender, hence generic) of object that each element in the collection will be. If a class Error is thrown ( because you try to put the wrong type of object in a collection) you will get the error where it is made, not miles later where you might have trouble figuring out what went wrong.
- 4. What is so great about generics?
-
Allows elements of type E to maintain their type E-ness while in the collection. ( rather than all being type Object, and later having to be recast to original type after it comes out of the collection again.). rom a compiler Pointof View, generics allows the compiler to check that the item added to the collection is of the correct type, and thus does a certain amount of the data checking before runtime, preventing runtime errors.
- 5. What happens if you pass a Comparator to the sort() method?
-
This overloaded method of sort() allows you to define by what parameter the sort will take place. Imagine an ArrayList of People. You can sort them by name, by occupation, by political perspective, by defining, within the People class, as a subclass, a comparator class for each type of comparison, and overriding the default compare() method in each class to decide how the new method should conduct the comparison.
- 6. What are the three main interfaces of Collection?
-
Set ( including SortedSet ), List and Map?
- 7. What are the differences between List, Set and Map?
-
A list is what I would think of as a list - an ordered collection or sequence of objects, and it may contain duplicate elements. Items in the list have precise order. Think: Pizza orders to fill: Large Everything, Medium Veggie Deluxe, Small Double cheese, Large Everything, Medium Meatatarian, Large Everything.
A set is a collection with no duplicates, that itemizes abstractions that can be manipulated. Think: Grocery list: Coffee, Oatmeal, Apples, Bread, Cheese, Turkey Meat, Mayo, Pasta, Spaghetti Sauce, SaladStuff, Wine. You want every item in the collection, but you don't list multiples separately, because it's not the individual items you are thinking of, but the abstract notion of each, to be sure each type thing is included. Good for methematical-type collections.
A Map is a collection that maps keys to values. Maps cannot contain duplicate keys, but they can contain duplicate values. Think Family Favourite Flavours: John - chocolate, Kris - Butter Pecan, Bonnie - maple walnut, Rob - Strawberry, Dan- chocolate, Keegan - Liver, Puddin- fish. The names are keys, and contain no duplicates, the flavours are values. Several keys can hold the same value, but each key can only hold one value.
- 8. What is the difference between reference equality and object equality?
-
Reference equality is the kind of equality referred to by
==. Object a == Object b if they reference the same object on the heap. If you create 2 string objects, and they each = "Tom, Dick, and Harry", they hold the same vlaue in the strings, but are not ==, because they are 2 separate objects, with their own address on the heap. You have to explicitly set them ==, even though they have the same state.
Object equality is determined by using the equals() method that each Object has. The default equals() method, however, starts by checking reference equality. So two objects that do not have reference equality will not, by default, have object equality. But sometimes, in your program, you just need your definition of Object equality to encompass certain values or state of your objects, and you want your program to consider two Objects as equal if they have the same state. Think identical twins. They are NOT the same person, bu perhaps they have the same colouring and height. They will not have reference equality. But perhaps for your program, you only care that they have the same state - colouring and height, and you want your program to consider them equal ( to have object equality) if they have the same state . You can override the default equals() method, and rewrite it into a version that only cares about state of the objects in your class, and this will give the two twins object equality. ( You will also need to override the hashCode() method, because the default method will check for hashCode equality, which may be different between the two twin Objects, and it will also check state.)
- 9. List any three Java Object laws for HashCode() and equals().
-
1. If two objects are equal, they have the same hashcodes. 2. Calling equals is associative, i.e if (x.equals(y)) then (y.equals(x)). 3. By default, hashcode() generates a different hashcode for every object on the heap, so when you start off comparing two objects they will never be equal by hashcode standards, even if they have the same state, and have reference equality. You need to override the default hashcode() to get a.hashCode() == b.hashCode().
- 10. True or false: Each element in a Map is actually two objects - a key, and a value?
-
True
- 11. True or false: In a Map you can have duplicate keys, but not duplicate values?
-
False - other way around. Each key is unique, eachvalue needn't be.