如何在JAVA一个ArrayList发现最少,最常见的名字 [英] How to find the least and most common name in an ArrayList in JAVA

查看:177
本文介绍了如何在JAVA一个ArrayList发现最少,最常见的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在一个ArrayList发现最多和最少的通用名称。该公式应通过名称的文件,并指望有多少共同的名字出现在列表中,然后打印最少,最常见的人。我已经有大部分的ArrayList部分完成,它只是找我有with.I不知道如何甚至开始它。我试图看看网上却找不到任何麻烦最多和最少的通用名称。我有点想弄明白但是这是我能想到的是用.equals的。

 的for(int i = 0; I< dogs.size();我++)
。如果dogs.get(0).getName()等于dogs.get(ⅰ).getName();
{}


解决方案

使用地图来收集数据,然后使用集合API来找到最小:

 列表<狗和GT;小狗; //填充
地图<字符串,整数>数=新的HashMap<>();
对于(狗狗:狗狗){
    整型数= counts.get(dog.getName());
    counts.put(dog.getName(),计数== NULL 1:计数+ 1);
}清单<&Map.Entry的LT;字符串,整数>>项=新的ArrayList<>(counts.entrySet());
Collections.sort(条目,新的比较<&Map.Entry的LT;字符串,整数>>(){
    公众诠释比较(Map.Entry的<字符串,整数> 01,为Map.Entry<字符串,整数> O2){
        返回Integer.compare(o2.getValue(),o1.getValue()); //注意反序
    }
});
串leastCommonName = entries.get(0).getKey();
INT leastCommonFrequency = entries.get(0).getValue();


下面是一个Java版本8查找使用最少的名称:

  Map.Entry的<字符串,整数>分= counts.entrySet()流()
    .min((O1,O2) - > Integer.compare(o1.getValue(),o2.getValue()))获得();串leastCommonName = min.getKey();
INT leastCommonFrequency = min.getValue();

从本质上讲列表的创建和排序是可以避免的,有一个班轮,从使用相同的比较器(的条目)流找到最小值所取代,但是作为一个lambda前pression。

I am having trouble finding the most and least common name in an ArrayList. The formula should go through a file of names and count how many common names there are in the list then print the least and most common of them. I already have the most ArrayList Part finished, it is just finding the most and least common name I am having trouble with.I have no idea how to even start it.I have tried to look online but couldn't find any. I kind of tried to figure it out but this is all I could think of is using .equals.

for (int i = 0; i< dogs.size(); i++)
if dogs.get(0).getName().equals dogs.get(i).getName();
{

}

解决方案

Use a Map to collect the data, then use the Collections API to find the minimum:

List<Dog> dogs; // populate
Map<String, Integer> counts = new HashMap<>();
for (Dog dog : dogs) {
    Integer count = counts.get(dog.getName());
    counts.put(dog.getName(), count == null ? 1 : count + 1);
}

List<Map.Entry<String, Integer>> entries = new ArrayList<>(counts.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
        return Integer.compare(o2.getValue(), o1.getValue()); // Note reverse order
    }
});
String leastCommonName = entries.get(0).getKey();
int leastCommonFrequency = entries.get(0).getValue();


Here's a java 8 version of finding the least-used name:

Map.Entry<String, Integer> min = counts.entrySet().stream()
    .min((o1, o2) -> Integer.compare(o1.getValue(), o2.getValue())).get();

String leastCommonName = min.getKey();
int leastCommonFrequency = min.getValue();

Essentially list creation and sort is avoided, replaced with a one-liner that finds the minimum value from a stream (of Entries) using the same comparator, but as a lambda expression.

这篇关于如何在JAVA一个ArrayList发现最少,最常见的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆