All Downloads are FREE. Search and download functionalities are using the official Maven repository.

z11.samplestructs.CountItem Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package z11.samplestructs;

import java.util.HashMap;

public class CountItem {
    public CountItem() {
        items = new HashMap();
    }
    
    public void addItem(String item) {
        if (items.containsKey(item)) {
            Long val = items.get(item);
            items.put(item, val + 1);
        } else {
            Long val = (long) 1;
            items.put(item, val);
        }
    }
    
    public String getTheMostItem() {
        String ch = "";
        long count_max = -1;
        for (HashMap.Entry item : items.entrySet()) {
            if (item.getValue() > count_max) {
                count_max = item.getValue();
                ch = item.getKey();
            }
        }
        return ch;
    }
    
    HashMap items;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy