Java实现Counter类【复制可用】

参考Python的Counter的实现,实现了Java版本的Counter


import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Breath
 * @date 3/6/2023 12:48 PM
 * @description 计数器
 */
public class Counter<T> {

    /*
    T:需要统计的元素的类型
    int[]: T出现的次数,之所以用int[]存储,是为了提高计数过程的性能,减少containKey的次数
     */
    private HashMap<T, int[]> counter = new HashMap<>();

    public Counter(Collection<T> collection) {
        if (collection != null) {
            for (Iterator<T> it = collection.iterator(); it.hasNext(); ) {
                T e = it.next();
                int[] valueWrapper = counter.get(e);
                if (valueWrapper == null) {
                    counter.put(e, new int[]{1});
                } else {
                    valueWrapper[0]++;
                }
            }
        }
    }

    public HashMap<T, int[]> getItems() {
        return this.counter;
    }

    public Set<T> getElementsSet() {
        return this.counter.keySet();
    }

    /**
     * 查看元素e的统计次数
     *
     * @param e
     * @return
     */
    public int getCount(T e){
        return this.getItems().get(e)[0];
    }

    /**
     * 存在 + 1,不存在直接设置为1
     * @param e 需要统计的元素,即key
     */
    public void put(T e) {
        int[] valueWrapper = counter.get(e);
        if (valueWrapper == null) {
            counter.put(e, new int[]{1});
        } else {
            valueWrapper[0]++;
        }
    }

    public int[] get(T e){
        return this.getItems().get(e);
    }

    public boolean containsE(T e){
        return this.counter.containsKey(e);
    }

    public boolean equalCounter(Counter<T> otherCounter) {
        if (otherCounter == null) {
            return false;
        }
        if (this.getItems().size() != otherCounter.getItems().size()) {
            return false;
        }
        for (Map.Entry<T, int[]> item : otherCounter.getItems().entrySet()) {
            if (!this.containsE(item.getKey()) || this.get(item.getKey())[0] != item.getValue()[0]) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Counter) {
            return this.equalCounter((Counter<T>) obj);
        }
        if (obj instanceof Collection){
            return equalCounter(new Counter<>((Collection<T>) obj));
        }
        return false;

    }

    public static void main(String[] args) {
        List<Integer> arr1 = Stream.of(1, 2, 2, 3, 4, 1, 5, 1).collect(Collectors.toList());
        List<Integer> arr2 = Stream.of(1, 2, 3, 1, 4, 2, 5, 1).collect(Collectors.toList());
        Counter<Integer> counter1 = new Counter<>(arr1);
        System.out.println(counter1.getItems());
        Counter<Integer> counter2 = new Counter<>(arr2);
        System.out.println(counter2.getItems());

        System.out.println(counter1.equals(counter2));
        System.out.println(counter2.equals(counter1));
        System.out.println(counter1.equals(new Counter<>(arr1)));
    }
}

你可能感兴趣的:(工具类,java工具类,实用为王,java,开发语言)