Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Whic ofthe JCF interfaces would be the most useful if we want to store a coll

ID: 3596343 • Letter: 1

Question

1. Whic ofthe JCF interfaces would be the most useful if we want to store a collection of students enrolled in COMP2402 so that we can quickly check if a student is enrolled in COMP2102? (a) Collection b)Set (c) SortedSet (d) Map (e) SortedMap 2. What if we also want to be able to quickly output a list of students, sorted by (lastname,firstname)? (a) Collection (b) Set cSortedSet (d) Map (e) SortedMap 3. Whatf, in addition, we also want to store some auxiliary information (c.g., a mark) with cach student? (a) Collection (b) Set (c) SortedSet (d) Map SortedMap 4. A Bag is like a Set except that equal elements can be stored more than once. Which of the following is best suited to implement a Bag? (a) Set (b) Map (c) Map (d) SortedSet eEither (b) or ( depending on what behaviour we want if we add two elements that are equal but not identical

Explanation / Answer

(1) Given Students we need to check if student is enrolled ; Best data structure is Set see why

Set<Student> comp2702 = new Set<>();

This set will have constin all the students enrolled in it, So we can just add that student and then add function will return 0 if its added otherwise it will add that student and return 1. So set is most useful collection here


(2) We need to Sort it, Since Set was used for that course . Java has SortedSet as well. So just sort them using Sorted Set

(3) Set<Student> set ; Set can store only one piece of information unlike map which stores key and value
Map<Student, Integer> or Map<Integer, Integer>. Such kind of facility map can provide so if we need to store in addition to some information we need to choose Map here and if we need sortedOrder based on highest /lowest marks, so SortedMap is useful here


(4) When equal element will be stored more than once, that means

2-> Count
2->2,2,2,2,2,2

So either we can Store Map<T, inetger> , here integer will give us count of that element
Or we can store element itself like Map<T, LIST<T>> SO 2ND PARAMETER WILL BE LIST

I hope things are clear, let me know if there is any doubt/concern/