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

I need help changing the below working methods into something a little more simp

ID: 645564 • Letter: I

Question

I need help changing the below working methods into something a little more simpler to understand, not neccesary shorter. I am still in my first Java semester and would like to stick to what I have learned so far. Please kindly comment every line. Thank you!

public static void countNames(ArrayList<String> names) {

        Set<String> set = new LinkedHashSet<String>(names);

        for (String name : set) {
            int count = Collections.frequency(names, name);

            System.out.println(name + " : " + count);

        }
    }

    public static void displayUniqueNames(ArrayList<String> names) {
        Set<String> set = new LinkedHashSet<String>(names);
        for (String name : set) {
            System.out.println(name);
        }
    }

Explanation / Answer

public static void countNames(ArrayList<String> names) {       //here countName is function name and public means it

        // can be used publicly anywhere , void means it donot return any thing

        Set<String> set = new LinkedHashSet<String>(names); //A Set is a Collection that cannot contain duplicate elements. It extends implements Set interface. set a new linked list which will store the string type elements

        for (String name : set) { //for loop run for every element of set
            int count = Collections.frequency(names, name); //it gives the frequency of elements from the list

            System.out.println(name + " : " + count);       //print the element and the no of elements in the list

        } //close for loop
    } //close countName function defination

    public static void displayUniqueNames(ArrayList<String> names) { // function which will print all name names of list elements
        Set<String> set = new LinkedHashSet<String>(names);   //delclare new linked list
        for (String name : set) {         //loop which will run for each element of list
            System.out.println(name);   //print name of list element
        }         //close for loop
    }      //close display function defination

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