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

Declare an ArrayList of Strings. Add 5 names to the collection. \"Bob\" \"Susan

ID: 3805177 • Letter: D

Question

Declare an ArrayList of Strings. Add 5 names to the collection. "Bob" "Susan Output the Strings onto the console using the enhanced for loop 3. import java util. 4. public class Assignment8 {5. 6. public static void main (String[] args) {7.//TODO Auto-generated method stub 8. Array List new Array list(); 9. Add(Bob) 10. a Susan 11. a add(Jeremy) 12. a. add Emil 13. Laura 14 for (object obj: a) The enhanced For Loop f 15. 16. 17.} Sort the list using the method Collections sort, Output the sorted List. Shuffle the list, and output the shuffled list. Note that Collections (with an s is a class, while Collection is an interface. The Collections class has many useful static methods for processing interfaces, including the sort method Search for the name "Susan" in the list. What location was it found? Search for a names that is not in the list. What location is reported? Describe why an equals method and a compare To method are required to achieve searching and sorting of the elements of a list. Convert the list above to an array using to Array Output the elements of the array. Convert the array back into a list using asList. Output the elements of the list. Describe the differences between a HashSet. TreeSet, and a LinkedHashSet. When might you choose each one of these? Describe the differences between a HashMap, TreeMap, and LinkedHashMap Describe the situation when might you use each of these?

Explanation / Answer

Hi, I have answered first 3 Questions.

Please repost others in separate post.

Please let me know in case of any issue.

package year_2017.march.fourthweek;

import java.util.ArrayList;

import java.util.Collections;

public class Assignment8 {

  

   public static void main(String[] args) {

       // 2

       ArrayList<String> a = new ArrayList<>();

       a.add("Bob");

       a.add("Susan");

       a.add("Jeremy");

       a.add("Emily");

       a.add("Laura");

      

       for(String s : a)

           System.out.println(s);

      

       //3.

       System.out.println(" Sorted list: ");

       Collections.sort(a);

       for(String s : a)

           System.out.println(s);

       System.out.println();

      

       //4.

      

       int index = Collections.binarySearch(a, "Susan");

       System.out.println(index);

       index = Collections.binarySearch(a, "Pravesh");

       System.out.println(index);

      

      

   }

}

/*

Bob

Susan

Jeremy

Emily

Laura

Sorted list:

Bob

Emily

Jeremy

Laura

Susan

4

-5 => this is the value where you can insert at (-5+1) index in list

*/

public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key)

Returns:
the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found

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