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

What is the output of the following code segments? Set<Integer> s = newHashSet<I

ID: 3610425 • Letter: W

Question

What is the output of the following code segments?    Set<Integer> s = newHashSet<Integer>();    s.add(3); 1.System.out.println(s.add(7));    s.add(9); 2.System.out.println(s.add(3));    Iterator<Integer> i =s.iterator();    while(i.hasNext()); 3.     System.out.print(i.next()); 1.____________ 2.____________ 3.____________ What is the output of the following code segments?    Set<Integer> s = newHashSet<Integer>();    s.add(3); 1.System.out.println(s.add(7));    s.add(9); 2.System.out.println(s.add(3));    Iterator<Integer> i =s.iterator();    while(i.hasNext()); 3.     System.out.print(i.next()); 1.____________ 2.____________ 3.____________

Explanation / Answer

Dear,   
  1. A Set represents a mathematical set.
  2. It is a Collection that, unlike List, does not allowduplicates.
  3. There must not be two elements of a Set, say e1 and e2, suchthat e1.equals(e2).
  4. The add method of Set returns false if you try to add aduplicate element.
   Set<Integer> s = newHashSet<Integer>(); // initialize set withintegers    s.add(3);                                                      //{3}     1.System.out.println(s.add(7));                        //{7,3}    s.add(9);                                                     //{9,7,3} 2.System.out.println(s.add(3));                        //{9,7,3}    Iterator<Integer> i =s.iterator();    while(i.hasNext()); 3.     System.out.print(i.next());                       //{7,3,9} 1.___   {7,3}_________ 2.___false_{9,7, 3}________ 3.____{7, 3, 9}________ the Set object rejects the duplicate, so the first itementered is kept (although in this case it would be hard to tellwhich one was actually stored) I hope this will helsp you!!!!!! the Set object rejects the duplicate, so the first itementered is kept (although in this case it would be hard to tellwhich one was actually stored) I hope this will helsp you!!!!!!
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote