package com.wf.bean;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test5 {
/**
* @param args
*/
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
System.out.println(frequency(list, "a")); // 3
System.out.println(frequency(list, "b")); // 2
System.out.println(frequency(list, "c")); // 1
System.out.println(frequency(list, "d")); // 5
System.out.println(frequency(list, "xxx")); // 0
}
public static int frequency(List<String> list, String s) {
int count = 0;
Iterator<String> it = list.iterator();
while (it.hasNext()) {
if (it.next().equals(s)) {
count++;
}
}
return count;
}
}
|
|