Idea: You can first check the original collection and output it to a new collection, and then use the new collection with the duplicates removed to traverse the original collection, and the double-layer for loop to get the number of repetitions and add it to the new one Output class (this class will be introduced below), after executing a full for loop, add the value and number of times to the set of newly created output classes, and reset num to zero to facilitate the next record. Then print it.
If you want to print the number of occurrences of a string and the corresponding string, you can build a new collection to receive these content, so I wrote a StringTimes class to receive the content, and rewritten the toString method to print it.
publicclassStringTimes{
private String str;private int num;publicStringTimes(String str, int num){
super();this.str = str;this.num = num;}
@Override
public String toString(){
return"[str="+ str +", num="+ num +"]";}public String getStr(){
return str;}publicvoidsetStr(String str){
this.str = str;}public int getNum(){
return num;}publicvoidsetNum(int num){
this.num = num;}}
Method of processing
publicstatic ArrayList<StringTimes>repeatString(ArrayList<String> str1){
ArrayList<StringTimes> str4 =newArrayList<StringTimes>();
int num =0;
ArrayList<String> str2 =newArrayList<String>();for(int i =0; i < str1.size(); i++){
String str3 = str1.get(i);if(!str2.contains(str3)){
str2.add(str3);}}for(int i =0; i < str2.size(); i++){
for(int j =0; j < str1.size(); j++){
if(str2.get(i).equals(str1.get(j))){
num++;}}
str4.add(newStringTimes(str2.get(i),num));
num =0;}return str4;}