ch07 - 1.1 Explain the effect of the following method...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1.1 Explain the effect of the following method calls. Set<String> s = new HashSet<String>(); s.add("hello"); s.add("bye"); s.addAll(s); Set<String> t = new TreeSet<String>(); t.add("123"); s.addAll(t); System.out.println(s.containsAll(t)); System.out.println(t.containsAll(s)); System.out.println(s.contains("ace")); System.out.println(s.contains("123")); s.retainAll(t); System.out.println(s.contains("123")); t.retainAll(s); System.out.println(t.contains("123")); Set<String> s = new HashSet<String>(); Creates a HashSet object to hold Strings and assignes a reference to it to the variable s. s.add("hello"); Adds the string "hello" to the set. s.add("bye"); Adds the string "bye" to the set. s.addAll(s); Copies the content of s into itself. Effectively doing nothing since sets do not contain duplicate values. Set<String> t = new TreeSet<String>(); Creates a TreeSet object to hold Strings and assignes a reference to it to the variable t. t.add("123"); Adds the string "123" to t. s.addAll(t); Adds the contents of t to s. s now contains {"hello", "bye", "123"} System.out.println(s.containsAll(t)); Outputs "true" since all of the elements in t are included in s. System.out.println(t.containsAll(s)); Outputs "false" since there are elements in s that are not in t. System.out.println(s.contains("ace")); Outputs "false" since "ace" is not in s. System.out.println(s.contains("123")); Outputs "true" since "123" is contained in s. s.retainAll(t); Removes the elements from s that are not in t. System.out.println(s.contains("123")); Outputs "true" since "123" is still in s. t.retainAll(s); Effectively nothing, since s now only contains the values in t. System.out.println(t.contains("123")); Outputs "true" since "123" is still in t. 1.2 What is the relationship between the Set interface and the Collection interface? The Set interface is a sub-interface of Collection, but does not define any additional methods. However, additional requirements are placed on constructors and the add, equals, and hashCode
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
methods. These additional requirements specify that a Set may not contain duplicate elements, and that the equals method verify that the other object is a Set. Finally the Set.hashCode method is specified to be the sum of the hashCodes of the elements, while the Collection.hashCode method has no such restriction. 1.3 What are the differences between the Set interface and the List interface? Elements in a List have a position (index) and the List interface contains methods to access element using an index. Also the requirements on the equals and hashCode methods are different. The List also defines a ListIterator which allows bi-directional traversal. 1.4 In Example 7.1, why is setAcopy needed? What would happen if you used the statement setAcopy = setA; to define setAcopy ? The variable setAcopy would reference the same object as setA, thus changes to one would be changes to
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

ch07 - 1.1 Explain the effect of the following method...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online