Does anyone know how can I get the following answer with the code I have?
aaa: 3
bbb: 2
ccc: 1
1: 2
2: 2
3: 1
4: 1
Here is what I've tried so far:
this is the main class
package tester1;
import java.util.ArrayList;
public class Tester1 {
public static void main(String[] args) {
tester t1 = new tester(1,"aaa");
tester t2 = new tester(2,"aaa");
tester t3 = new tester(2,"aaa");
tester t4 = new tester(1,"ccc");
tester t5 = new tester(3,"bbb");
tester t6 = new tester(4,"bbb");
ArrayList<tester> list = new ArrayList<tester>();
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
list.add(t5);
list.add(t6);
test t = new test(list);
t.getter();
}
}
this the the class to connect to the array list
package tester1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class test {
private ArrayList<tester> testList;
public test(ArrayList<tester> testList) {
this.testList = testList;
}
public void getter()
{
Set<tester>unique = new HashSet<tester>(testList);
for(tester key:unique)
{
System.out.println(key.getName()+": "+Collections.frequency(testList, key.getName()));
}
}
}
this class the where the constructor is
package tester1;
public class tester {
private int num;
private String name;
public tester(int num, String name) {
this.num = num;
this.name = name;
}
public int getNum() {
return num;
}
public String getName() {
return name;
}
}