Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Displaying multiple max occurrences in a collection.

807589Oct 23 2008 — edited Oct 24 2008
Hello, I've written a program that asks the user to input numbers. Once 0 is entered, input ends. The values are then stored in a map, along with their occurrences. I need it to display the value that has the max occurrence only, which I have achieved. Problem is, I also need it to display mutliple values if their occurrence(max) is the same. Any thoughts?
import java.util.*;

public class NumberOccurrence {
    

    public static void main(String[] args) {
        
        LinkedList<Integer> valueList = new LinkedList<Integer>();
        int input = 1;
        Scanner Keyboard = new Scanner(System.in);
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();  
        
        while (input != 0) {
            System.out.println("Enter a value. If the value 0 is " +
                    "entered, input ends.");
            input = Keyboard.nextInt();
            valueList.add(input);        
        }
        
        ListIterator<Integer> listIterator = valueList.listIterator();
        
        while (listIterator.hasNext()) {
        
                 int key = listIterator.next();
            
                if (map.get(key) == null) {
                    map.put(key, 1);
                }
                else {
                    int value = map.get(key).intValue();
                    value++;
                    map.put(key, value);
                }
        }

        ArrayList<sortedOccurrence> list = new ArrayList<sortedOccurrence>();
        
        Set<Map.Entry<Integer, Integer>> entrySet = map.entrySet();
        
        for (Map.Entry<Integer, Integer> entry: entrySet) {
            list.add(new sortedOccurrence(entry.getKey(), entry.getValue()));
        }
        
        Collections.sort(list, Collections.reverseOrder());
     
        
        System.out.println(Collections.max(list));
    
    }    
}

class sortedOccurrence implements Comparable<sortedOccurrence> {
    private int number;
    private int count;
    
    public sortedOccurrence(int number, int count) {
        this.number = number;
        this.count = count;
    }
    public int compareTo(sortedOccurrence o) {
        return count-o.count;
    }
    public String toString() {
        return "The number is " + number + " \t" + "The count is " + count;
    }    
}
If I were to input 5, 5, 4, 4, 3, 2, 1 and 0, the display would look like this:

"The number is 4 The count is 2"

What I need it to do is display multiple values that have the same (max) occurrence. Output should look like this:

"The number is 4 The count is 2
The number is 5 The count is 2"

Any help would be much appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2008
Added on Oct 23 2008
2 comments
664 views