How to count the number of occurrences of an element in a List

后端 未结 22 1211
一生所求
一生所求 2020-11-22 12:25

I have an ArrayList, a Collection class of Java, as follows:

ArrayList animals = new ArrayList();
animals.add(\"bat\         


        
相关标签:
22条回答
  • 2020-11-22 12:37

    Put the elements of the arraylist in the hashMap to count the frequency.

    0 讨论(0)
  • 2020-11-22 12:37
    List<String> lst = new ArrayList<String>();
    
    lst.add("Ram");
    lst.add("Ram");
    lst.add("Shiv");
    lst.add("Boss");
    
    Map<String, Integer> mp = new HashMap<String, Integer>();
    
    for (String string : lst) {
    
        if(mp.keySet().contains(string))
        {
            mp.put(string, mp.get(string)+1);
    
        }else
        {
            mp.put(string, 1);
        }
    }
    
    System.out.println("=mp="+mp);
    

    Output:

    =mp= {Ram=2, Boss=1, Shiv=1}
    
    0 讨论(0)
  • 2020-11-22 12:42

    To get the occurrences of the object from the list directly:

    int noOfOccurs = Collections.frequency(animals, "bat");
    

    To get the occurrence of the Object collection inside list, override the equals method in the Object class as:

    @Override
    public boolean equals(Object o){
        Animals e;
        if(!(o instanceof Animals)){
            return false;
        }else{
            e=(Animals)o;
            if(this.type==e.type()){
                return true;
            }
        }
        return false;
    }
    
    Animals(int type){
        this.type = type;
    }
    

    Call the Collections.frequency as:

    int noOfOccurs = Collections.frequency(animals, new Animals(1));
    
    0 讨论(0)
  • 2020-11-22 12:46

    ​If you use Eclipse Collections, you can use a Bag. A MutableBag can be returned from any implementation of RichIterable by calling toBag().

    MutableList<String> animals = Lists.mutable.with("bat", "owl", "bat", "bat");
    MutableBag<String> bag = animals.toBag();
    Assert.assertEquals(3, bag.occurrencesOf("bat"));
    Assert.assertEquals(1, bag.occurrencesOf("owl"));
    

    The HashBag implementation in Eclipse Collections is backed by a MutableObjectIntMap.

    Note: I am a committer for Eclipse Collections.

    0 讨论(0)
  • 2020-11-22 12:47

    I didn't want to make this case more difficult and made it with two iterators I have a HashMap with LastName -> FirstName. And my method should delete items with dulicate FirstName.

    public static void removeTheFirstNameDuplicates(HashMap<String, String> map)
    {
    
        Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
        Iterator<Map.Entry<String, String>> iter2 = map.entrySet().iterator();
        while(iter.hasNext())
        {
            Map.Entry<String, String> pair = iter.next();
            String name = pair.getValue();
            int i = 0;
    
            while(iter2.hasNext())
            {
    
                Map.Entry<String, String> nextPair = iter2.next();
                if (nextPair.getValue().equals(name))
                    i++;
            }
    
            if (i > 1)
                iter.remove();
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 12:49

    Actually, Collections class has a static method called : frequency(Collection c, Object o) which returns the number of occurrences of the element you are searching for, by the way, this will work perfectly for you:

    ArrayList<String> animals = new ArrayList<String>();
    animals.add("bat");
    animals.add("owl");
    animals.add("bat");
    animals.add("bat");
    System.out.println("Freq of bat: "+Collections.frequency(animals, "bat"));
    
    0 讨论(0)
提交回复
热议问题