Finding most occurrences of objects by a specific field in an arraylist in java

I have an arraylist of objects that contain information about calls made to a specific destination number. I was trying to find a better way to search this list and return the number with the most occurrences, as well as the number of those occurrences (called from another method in a related class).

For example:

I have this method which adds calls to a 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 then I need to be able to search for the arraylist callHistory, it updates and finds the destination that was called the most times and returns that number and count.

Then I will call these values ​​for every "phone" the person has and print the destination with the highest count of all "phones" and also count.

I went through and found information about looking for occurrences of a specific object, but couldn't figure out how to check for a specific field within that object and not on the object itself.

Sorry if this sounds confusing, but I am very confused and ended up with ideas, my hash mapping is not very strong yet, and I could not tweak the examples that I found do what I want.

based on the comments below i

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 don't know how to use the Phonecall destination field instead of the object itself.

Would something like this be more appropriate:

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);
    }

      

+3


source to share


2 answers


One solution would be to declare Map<String, Integer> phoneCount

that would contain the phone number as the key and the number of calls made to that number as the value.



Then you will walk through the objects ArrayList

from PhoneCall

and create a map. The biggest metric is the one you are looking for.

+2


source


For anyone looking 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);
    }

      

0


source







All Articles