Count number of items with property

后端 未结 10 2546
不思量自难忘°
不思量自难忘° 2021-02-13 12:18

I have list List where Custom is like

class Custom{
    public int id;
    public String name;
}

How to

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 13:03

    What about this? :

    package test;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    
    public class CustomArrayBuilder extends ArrayList {
    
        Map namesMap = new HashMap();
    
        public CustomArrayBuilder(Collection c) {
            super(c);
            this.prepareAddAll(c);
        }
    
        public int getDifferentNamesAmount() {
            return this.namesMap.size();
        }
    
        public int getNameAmount(String name) {
            Integer integer = this.namesMap.get(name);
            return (integer != null) ? integer : 0;
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public Custom set(int index, Custom element) {
            Custom custom = super.set(index, element);
            prepareSet(custom, element);
            return custom;
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean add(Custom e) {
            this.prepareAdd(e);
            return super.add(e);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void add(int index, Custom element) {
            this.prepareAdd(element);
            super.add(index, element);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public Custom remove(int index) {
            Custom custom = super.remove(index);
            this.prepareRemove(custom);
            return custom;
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void clear() {
            super.clear();
            this.namesMap.clear();
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean addAll(Collection c) {
            this.prepareAddAll(c);
            return super.addAll(c);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean addAll(int index, Collection c) {
            this.prepareAddAll(c);
            return super.addAll(index, c);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean remove(Object o) {
            if (super.remove(o)) {
                this.prepareRemove((Custom) o);
                return true;
            } else {
                return false;
            }
        }
    
        private void prepareSet(Custom oldCustom, Custom newCustom) {
            if (oldCustom != null && !oldCustom.name.equals(newCustom.name)) {
                this.prepareRemove(oldCustom);
                this.prepareAdd(newCustom);
            }
        }
    
        private void prepareAdd(Custom custom) {
            if (custom != null) {
                Integer integer = this.namesMap.get(custom.name);
                this.namesMap.put(custom.name, (integer != null) ? integer + 1 : 1);
            }
        }
    
        private void prepareAddAll(Collection c) {
            for (Custom custom : c) {
                this.prepareAdd(custom);
            }
        }
    
        private void prepareRemove(Custom custom) {
            if (custom != null) {
                Integer integer = this.namesMap.get(custom.name);
                this.namesMap.put(custom.name, (integer != null && integer > 0) ? integer - 1 : 0);
            }
        }
    }
    

    Usage:

    package test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Test {
    
        public static void main(String[] args) {
    
            List list = new ArrayList() {{
                add(new Custom("A"));
                add(new Custom("B"));
                add(new Custom("C"));
                add(new Custom("A"));
                add(new Custom("A"));
                add(new Custom("B"));
            }};
    
            CustomArrayBuilder customs = new CustomArrayBuilder(list);
            Custom custom = new Custom("B");
            customs.add(custom);
            customs.add(custom);
            customs.remove(custom);
            customs.remove(custom);
            customs.remove(custom);
    
            System.out.println("A: " + customs.getNameAmount("A"));
            System.out.println("B: " + customs.getNameAmount("B"));
            System.out.println("C: " + customs.getNameAmount("C"));
            System.out.println("Z: " + customs.getNameAmount("Z"));
            System.out.println("Total different names: " + customs.getDifferentNamesAmount());
        }
    }
    

    Output:

    A: 3
    B: 2
    C: 1
    Z: 0
    Total different names: 3
    

    It could be usefull when you often use your count operations. Note: You shouldn't change name of custom object, it should be final:

    package test;
    
    class Custom {
        public int id;
        final public String name;
    
        public Custom(String name) {
            this.name = name;
        }
    }
    

    Or you must do something with list too, when you are changing name of some Custom object from the list.

提交回复
热议问题