22.1 | Describe the Java Collections Framework. List the interfaces, convenience abstract classes, and concrete classes. |
22.2 | Can a collection object be cloned and serialized? |
22.3 | The hashCode method and the equals method are defined in the Object class. Why are they redefined in the Collection interface? |
22.4 | Find the default implementation for the equals method and the hashCode method in the Object class from the source code of Object.java. |
22.5 | How do you create an instance of Set ? How do you insert a new element in a set? How do you remove an element from a set? How do you find the size of a set? |
22.6 | What are the differences between HashSet , LinkedHashSet , and TreeSet ? |
22.7 | How do you traverse the elements in a set? Can you traverse the elements in a set in an arbitrary order? |
22.8 | How do you sort the elements in a set using the compareTo method in the Comparable interface? How do you sort the elements in a set using the Comparator interface? What would happen if you added an element that cannot be compared with the existing elements in a tree set? |
22.9 | Suppose that set1 is a set that contains the strings "red" , "yellow" , "green" , and that set2 is another set that contains the strings "red" , "yellow" , "blue" . Answer the following questions:
|
22.10 | What are the differences between the Comparable interface and the Comparator interface? Which package is Comparable in, and which package is Comparator in? |
22.11 | The Comparator interface contains the equals method. Why is the method not implemented in the GeometricObjectComparator class in this section? |
22.12 | How do you add and remove elements from a list? How do you traverse a list in both directions? |
22.13 | Suppose that list1 is a list that contains the strings "red" , "yellow" , "green" , and that list2 is another list that contains the strings "red" , "yellow" , "blue" . Answer the following questions:
|
22.14 | What are the differences between ArrayList and LinkedList ? Are all the methods in ArrayList also in LinkedList ? What methods are in LinkedList but not in ArrayList ? |
22.15 | How do you create a set or a list from an array of objects? |
22.16 | Are all the methods in the Collections class static? |
22.17 | Which of the following static methods in the Collections class are for lists, and which are for collections? sort, binarySearch, reverse, shuffle, max, min, disjoint , frequency |
22.18 | Show the printout of the following code: import java.util.*; public class Test { public static void main(String[] args) { List<String> list = Arrays.asList( "yellow" , "red" , "green" , "blue" ); Collections.reverse(list); System.out.println(list); List<String> list1 = Arrays.asList( "yellow" , "red" , "green" , "blue" ); List<String> list2 = Arrays.asList( "white" , "black" ); Collections.copy(list1, list2); System.out.println(list1); Collection<String> collection1 = Arrays.asList( "red" , "cyan" ); Collection<String> collection2 = Arrays.asList( "red" , "blue" ); Collection<String> collection3 = Arrays.asList( "pink" , "tan" ); System.out.println(Collections.disjoint(collection1, collection2)); System.out.println(Collections.disjoint(collection1, collection3)); Collection<String> collection = Arrays.asList( "red" , "cyan" , "red" ); System.out.println(Collections.frequency(collection, "red" )); } } |
22.19 | Which method can you use to sort the elements in an ArrayList or a LinkedList ? Which method can you use to sort an array of strings? |
22.20 | Which method can you use to perform binary search for elements in an ArrayList or a LinkedList ? Which method can you use to perform binary search for an array of strings? |
22.21 | Write a statement to find the largest element in an array of comparable objects? |
22.22 | How do you create an instance of Vector ? How do you add or insert a new element into a vector? How do you remove an element from a vector? How do you find the size of a vector? |
22.23 | How do you create an instance of Stack ? How do you add a new element into a stack? How do you remove an element from a stack? How do you find the size of a stack? |
22.24 | Does Listing 22.1, TestHashSet.java, compile and run if line 7 ( Set set = new HashSet() ) is replaced by one of the following statements? Collection set = new LinkedHashSet(); Collection set = new TreeSet(); Collection set = new ArrayList(); Collection set = new LinkedList(); Collection set = new Vector(); Collection set = new Stack(); |
22.25 | Is java.util.Queue a subinterface of java.util.Collection , java.util.Set , or java.util.List ? Does LinkedList implement Queue ? |
22.26 | How do you create a priority queue for integers? By default, how are elements ordered in a priority queue? Is the element with the least value assigned the highest priority in a priority queue? |
22.27 | How do you create a priority queue that reverses the natural order of the elements? |
22.28 | How do you create an instance of Map ? How do you add a pair consisting of an element and a key into a map? How do you remove an entry from a map? How do you find the size of a map? How do you traverse entries in a map? |
22.29 | Describe and compare HashMap , LinkedHashMap , and TreeMap . |
22.30 | Show the printout of the following code: public class Test { public static void main(String[] args) { Map map = new LinkedHashMap(); map.put( "123" , "John Smith" ); map.put( "111" , "George Smith" ); map.put( "123" , "Steve Yao" ); map.put( "222" , "Steve Yao" ); System.out.println( "(1) " + map); System.out.println( "(2) " + new TreeMap(map)); } } |