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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.