- 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.print(frequency(list, "a") + " " + frequency(list, "b")
- + " " + frequency(list, "c") + " " + frequency(list, "d") + " "
- + frequency(list, "xxx"));
- }
- private static <T> int frequency(List<T> list, T t) {
- int count = 0;
- for (T t2 : list) {
- if (t.equals(t2)) {
- count++;
- }
- }
- return count;
- }
复制代码
|
|