[JAVA] Class to take count

As you solve the problem, you may get a count.

Use an array

When the maximum value is known and can be secured in the array.

Main.java


    public static void main(String[] args) {
        int[] ary = { 1, 2, 2, 3, 3, 3, 9, 8 };
        int[] cnt = new int[10];
        for (int v : ary) {
            cnt[v]++;
        }
        for (int i = 0; i < cnt.length; i++) {
            if (cnt[i] > 0) {
                System.out.println(i + ":cnt=" + cnt[i]);
            }
        }
    }

When you run ...

1:cnt=1
2:cnt=2
3:cnt=3
8:cnt=1
9:cnt=1

Counter class that uses HashMap

Use HashMap if you don't know the maximum value, or if it's too big to reserve in an array. Of course, the number of registrations shall fit in the memory.

Counter.java


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Counter<T> {
    private Map<T, Integer> map = new HashMap<>();

    public void add(T key) {
        Integer v = map.get(key);
        if (v == null) {
            map.put(key, 1);
        } else {
            map.put(key, v + 1);
        }
    }

    public int get(T key) {
        Integer v = map.get(key);
        if (v == null) {
            return 0;
        } else {
            return v;
        }
    }

    public Set<T> keySet() {
        return map.keySet();
    }
}

How to use

Main.java


    public static void main(String[] args) {
        int[] ary = { 1, 22, 22, 333, 333, 333, 9999, 8888 };
        Counter<Integer> c = new Counter<>();
        for (int v : ary) {
            c.add(v);
        }
        for (Integer i : c.keySet()) {
            System.out.println(i + ":cnt=" + c.get(i));
        }
    }

When you run ...

1:cnt=1
22:cnt=2
8888:cnt=1
333:cnt=3
9999:cnt=1

Modified Counter class

(1/24 postscript) I got information that a method called Map.getOrDefault was added from JDK 1.8, so I rewrote it.

Counter.java


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Counter<T> {
    private Map<T, Integer> map = new HashMap<>();

    public void add(T key) {
        int v = get(key);
        map.put(key, v + 1);
    }

    public int get(T key) {
        return map.getOrDefault(key, 0);
    }

    public Set<T> keySet() {
        return map.keySet();
    }
}

Previously, if int v = map.get (key) ;, if it was null, it would be nullpo, so there was no choice but to receive it as an Integer and judge it, but it is refreshing to be able to return the default value. In addition, 0 of the second argument of getOrDefault is cached when autoboxing to Integer.

In C ++

(1/21 postscript) In C ++, the primitive type can be specified in map, so it is super easy. In addition, the operator [] is overloaded. This is sly.

main.cpp


    std::map<int,int> c;
    c[ary[i]]++;

With java

(1/21 postscript) If it is java, it will be null when it is accessed for the first time, so it is absolutely necessary to judge the first and second times. I thought about creating an Entry class in the inner class of Counter and having int cnt ;, but if an instance of the number of elements in the map is created and the count is within 127, it is more efficient than Integer cached from -128 to 127. bad. However, for 128 or more, if there is no other corresponding element, 128 instances are created, 129 instances are created, and 128 instances are subject to GC. Then, if the count is up to 127, Integer will be used, and from 128, the int value of the own Entry class will be added. I don't think it will be much faster if you do that. So boot option -XX: AutoBoxCacheMax → I read the source of Integer

Recommended Posts

Class to take count
How to use java class
[Ruby] Method to count specific characters
How to disassemble Java class files
Kotlin Class to send to Java developers
How to use the wrapper class
Baseball ball count (how to write)
How to decompile java class files
[Java] How to use LinkedHashMap class
How to use class methods [Java]
to_ ○
[Java] How to use Math class
Kotlin Class part.2 to send to Java developers
[Java] How to use the File class
[Kotlin] 3 ways to get Class from KClass
[Java] How to use the HashMap class
Item 23: Prefer class hierarchies to tagged classes
Ruby length, size, count How to use
How to define an inner class bean
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
Anonymous class (aiming to introduce stream api)
[Java] How to use the Calendar class
How to count UTF-8 code points fast