在java中通过arraylist中的特定字段查找大多数对象

在java中通过arraylist中的特定字段查找大多数对象,java,algorithm,Java,Algorithm,我有一个对象的arraylist,其中包含对特定目标号码的呼叫信息。我一直在试图找出搜索此列表的最佳方法,并返回出现次数最多的数字以及这些出现次数的计数(将从链接类中的另一个方法调用) 例如: 我有一个方法,通过调用地址簿中的随机数,将调用添加到列表中 public void makeCall(Phonecall call) { call = new Phonecall(); call.setDestination(anyNumber()); call.se

我有一个对象的arraylist,其中包含对特定目标号码的呼叫信息。我一直在试图找出搜索此列表的最佳方法,并返回出现次数最多的数字以及这些出现次数的计数(将从链接类中的另一个方法调用)

例如:

我有一个方法,通过调用地址簿中的随机数,将调用添加到列表中

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);
    }
然后我需要能够搜索它正在更新的arraylist callHistory,找到被调用次数最多的目的地,并返回该数字和计数

然后,我将为一个人拥有的每个“电话”调用这些值,并打印所有“电话”中计数最高的目的地及其计数

我一直在四处寻找,找到了关于查找某个特定对象的实例的信息,但没有弄清楚如何检查该对象中的特定字段,而不是对象本身

很抱歉,如果这听起来很复杂,但我已经很困惑,已经没有想法了,我的哈希映射还不是很强,我无法调整我找到的示例来实现我想要的

根据下面的评论,我有

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);
    }
public void mostCalled(字符串[]args)
{
Map Map=newhashmap();

对于(int i=0;i一种解决方案是声明一个
Map phoneCount
,它将电话号码作为键,并将拨打该号码的电话数作为值


然后,您将循环查看
PhoneCall
对象的
ArrayList
,并构建映射。具有最大值的记录就是您要查找的记录。

对于其他任何想要这样做的人,这就是我最终得到的结果

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);
    }
public void mostcall()
{
Map Map=newhashmap();
用于(电话呼叫:呼叫历史记录)
{
整数计数=map.get(call.destination);
put(call.destination,count==null?1:count+1);
}  
List maxKeyList=新建ArrayList();
整数最大值=整数最小值;
对于(Map.Entry:Map.entrySet())
{
if(entry.getValue()>maxValue)
{
maxValue=entry.getValue();
add(entry.getKey());
}
}
System.out.println(“呼叫最多的电话号码:“+maxKeyList”);
}

使用
映射
将调用作为键和值作为计数。迭代您的ArrayList,同时填充映射。为了回答您的最后一个问题,我们最好查看PhoneCall声明。但是,我建议您不要使用带有计数器的for扫描列表,您最好使用迭代器。如果ArrayList成为LinkedList,然后您将了解它有多好!添加了另一个尝试,使用字符串代替对象,使用迭代器代替计数器。这看起来更像我应该从中开始的吗?我将如何存储“目标”PhoneCall对象的字段作为键,而不是对象本身?我将其声明为Map,例如您可以输入目标电话号码,该号码可能是一个字符串。
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);
    }