Anthony Fawkes
Anthony Fawkes

Reputation: 55

Finding most occurences of objects by a particular field in an arraylist in java

I have an arraylist of objects which contain information on calls made to a particular destination number. I have been trying to figure out the best way to search this list and return the number with the most occurrences as well as the count of these occurrences (to be called from another method in a linked class).

For instance:

I have this method which adds calls to the list by calling a random number in the address book

public void makeCall(Phonecall call)
    {
     call = new Phonecall();
     call.setDestination(anyNumber());
     call.setDuration(0 + (int)(Math.random() * (((balance/25) * 60) - 0) + 1));
     double cost = (call.getDuration()/60 * 25);
     balance = getBalance() - cost;
     updateCallHistory(call);
    }

and I then need to be able to search the arraylist callHistory it is updating and find the destination that has been called the most number of times and return that number and count.

Then I will call these values for each "phone" a person has and print the destination with the highest count across all "phones" as well as it's count.

I've been looking around and have found information on finding occurrences of a particular object, but not been able to figure out how to check a particular field within that object instead of the object itself.

Sorry if this sounds convoluted but I'm getting quite confused and have run out of ideas, my hash mapping isn't very strong yet and I couldn't tweak the examples I've found to do what I want.

based on comments below I have

public void mostCalled(String[] args) 
    {
        Map<Phonecall,Integer> map = new HashMap<Phonecall, Integer>();  
        for(int i=0;i<callHistory.size();i++){              
            Integer count = map.get(callHistory.get(i));         
            map.put(callHistory.get(i), count==null?1:count+1);  
        }  
        System.out.println(map);
    }

but I do not know how to use the destination field of Phonecall instead of the object itself.

Would something like this be more suitable:

public void mostCalled(String[] args) 
    {
        Map<String,Integer> map = new HashMap<String, Integer>();  
        for(Phonecall call : callHistory)
        {
            Integer count = map.get(call.destination);         
            map.put(call.destination, count==null?1:count+1);  
        }  
        System.out.println(map);
    }

Upvotes: 3

Views: 358

Answers (2)

Anthony Fawkes
Anthony Fawkes

Reputation: 55

For anyone else that wants to do this, this is what I ended up with.

public void mostCalled() 
    {
        Map<String,Integer> map = new HashMap<String, Integer>();  
        for(Phonecall call : callHistory)
        {
            Integer count = map.get(call.destination);         
            map.put(call.destination, count==null?1:count+1);  
        }  
        List<String> maxKeyList=new ArrayList<String>();
        Integer maxValue = Integer.MIN_VALUE; 
        for(Map.Entry<String,Integer> entry : map.entrySet()) 
        {
             if(entry.getValue() > maxValue) 
             {
                 maxValue = entry.getValue();
                 maxKeyList.add(entry.getKey());
             }
        }
        System.out.println("Phone numbers called the most : "+maxKeyList);
    }

Upvotes: 0

Dan D.
Dan D.

Reputation: 32391

One solution would be to declare a Map<String, Integer> phoneCount which would hold a phone number as the key and the number of calls made to this number as the value.

Then, you would loop through the ArrayList of PhoneCall objects and build the map. The record having the biggest value is the one you are looking for.

Upvotes: 2

Related Questions