{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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 ?
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern