Calucuting a max on an arbitrary property within a collection of objects?

Posted on 2006-04-13
Last Modified: 2013-11-23
I have a collection of Items.

say for example:  Collection items = new ArrayList();

Items class has a property called maxOccurrenceCount

I want to calcutate that which item has the maxOccurrenceCount out of the entire collection.

Thanx in advance.
Question by:slickdeals
    LVL 86

    Expert Comment

    Sort it using a Comparator on maxOccurrenceCount or, if it implements Comparable, you can do


    Author Comment

    No my object does not have a comparator.

    This is how the object looks like :

    class Item{
    private Integer maxOccurrenceCount;
    // gets and sets

    How can i add a comparator. thanx for the input.
    LVL 86

    Accepted Solution

    Collections.max(itemsCollection, new Comparator() {
          public int compare(Object o1, Object o2) {
                return ((Item)o1).getMaxOccurrenceCount().compareTo((Item)o2).getMaxOccurrenceCount());
    LVL 5

    Assisted Solution

    Iterator iterator = items.iterator();
    Item maxItem = (Item);

       Item nextItem = (Item);
       if(nextItem.getMaxOccurencCount() > maxItem.getMaxOccurenceItem())
           maxItem = nextItem;

    return maxItem;

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    bigHeights  challenge 13 43
    bunnyEars challenge 6 47
    count7 challenge 12 53
    endX challenge 2 33
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now