我需要获取对象的arrayList中最频繁出现的元素的计数。我有这个代码,它的工作。ArrayList中的重复元素(java)
public static int contarRepeditos(ArrayList<Objecto> a) {
int resul = 0;
ArrayList<Integer> valores = new ArrayList<Integer>();
for (int i = 0; i < a.size(); i++) {
valores.add(a.get(i).getValor());
}
ArrayList<Integer> resultados = new ArrayList<Integer>();
for (int i = 0; i < valores.size(); i++) {
resultados.add(Collections.frequency(valores, a.get(i).getValor()));
}
resul = Collections.max(resultados);
return resul;
}
我需要知道是否有最佳方法来做到这一点。谢谢。
您的代码不符合您的描述。这听起来像你需要得到最频繁发生的'valor'值 - 这是这种情况吗? –
是的,对不起,我很难用英语表达这一点。是最频繁的。例如具有(0)(0)(0)(1)(1)的5个对象。你得到3. – ramons03