Java 从数组中获取最频繁的值

Java 从数组中获取最频繁的值,java,arrays,arraylist,Java,Arrays,Arraylist,我有一个如下所示的数组 String[] football_club = {"Barcelona", "Real Madrid", "Chelsea", "Real Madrid", "Barcelona", "Barcelona"}; //result Sting result_club = "a value most in the array" 从上面的数组中可以看到“Barcelona”,其值通常存在于数组中。 如何通过编码查找数组中最常出现的值?您可以制作一个HashMap。如果字符

我有一个如下所示的数组

String[] football_club = {"Barcelona", "Real Madrid", "Chelsea", "Real Madrid", "Barcelona", "Barcelona"};

//result
Sting result_club = "a value most in the array"
从上面的数组中可以看到“Barcelona”,其值通常存在于数组中。

如何通过编码查找数组中最常出现的值?

您可以制作一个
HashMap
。如果字符串已经出现在映射中,则将其键增加1,否则,将其添加到映射中

例如:

put("Barcelona", 1);
然后,假设又是“巴塞罗那”,你可以:

put("Barcelona", get("Barcelona") + 1);
因为“巴塞罗那”的关键是1,现在当你把它放进去的时候,关键是2

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Alexander.Iljushkin
 * Date: 23.10.13
 * Time: 11:32
 */
public class TestClass1 {


    public static void main(String[] args) {
        String[] football_club = {"Barcelona", "Real Madrid", "Chelsea", "Real Madrid", "Barcelona", "Barcelona"};
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        String tempStr;
        for (int i = 0; i < football_club.length; i++)
        {
            tempStr = football_club[i];
            if(map.containsKey(tempStr))
            {
                map.put(tempStr, map.get(tempStr) + 1);
            }
            else
            {
                map.put(tempStr,1);
            }
        }

        System.out.print(map.toString());
    }
}

然后,当您对hashmap进行排序时,只需获取第一个键,这将是最常用的键

您可以定义一个封装hashmap对象的新类


然后你可以定义你自己的put和get方法

另一种方法:对数组进行排序,然后对其进行迭代以计数连续的公共元素,并跟踪最大计数/对应元素。

排序可能比较繁重(相对而言),但我认为hashmap访问的复杂程度相同(O(n.log(n))。

如果性能不重要:

        String[] football_club = {"Barcelona", "Real Madrid", "Chelsea", "Real Madrid", "Barcelona", "Barcelona"};              
        List<String> l = Arrays.asList(football_club);
        Set<String> s = new HashSet<String>(l);
        for (String key : s) {
            int count = Collections.frequency(l, key);
            System.out.println("Found '" + key + "' " + count + " times.");
        }
String[]football_club={“巴塞罗那”、“皇家马德里”、“切尔西”、“皇家马德里”、“巴塞罗那”、“巴塞罗那”};
列表l=数组.asList(足球俱乐部);
Set s=新哈希集(l);
for(字符串键:s){
int count=Collections.frequency(l,key);
System.out.println(“找到“+”键“+”计数“+”次”);
}

这非常有效,max是用于存储找到的max count的新变量,如果两个元素具有相同的频率字符串[]football\u club={“Chelsea”、“Chelsea”、“Real Madrid”、“Real Madrid”、“Real Madrid”、“Barcelona”、“Chelsea”}

这就是切尔西

private String getRepeatedString(String[] football_club) {
    List<String> List1 = Arrays.asList(football_club);
    HashMap<String, Integer> data = new HashMap<String, Integer>();
    int max = 0;
    for (String string : List1) {
        int count = 1;
        if (data.containsKey(string)) {
            count = data.get(string) + 1;
            data.put(string, count);
        } else {
            data.put(string, 1);
        }
        if (max < count)
            max =count;
    }
    System.out.println(data);
    for (Entry<String, Integer> entry : data.entrySet()) {
        if (max == entry.getValue()) {
            return entry.getKey();
        }
    }
    return null;
}
私人字符串getRepeatedString(字符串[]足球俱乐部){
List List1=Arrays.asList(足球俱乐部);
HashMap数据=新的HashMap();
int max=0;
for(字符串:List1){
整数计数=1;
if(data.containsKey(字符串)){
count=data.get(字符串)+1;
data.put(字符串、计数);
}否则{
data.put(字符串,1);
}
如果(最大值<计数)
最大值=计数;
}
系统输出打印项次(数据);
for(条目:data.entrySet()){
if(max==entry.getValue()){
return entry.getKey();
}
}
返回null;
}

请帮助我了解您的问题。是否要计算一个单词在数组中重复的次数?请检查此项。希望它能帮助您。您应该提供更多有关问题的详细信息以及您遇到问题的位置。仅向问题询问代码可能会被视为stackoverflow的主题之外(请参阅)@blackbelt:是的,然后会显示一个数组值。你能帮我吗?谢谢you@user1878292更改了答案。然后,当您对hashmap进行排序后,只需获取第一个键,这将是最常用的键。我得到了它,这是真的。非常感谢您的回答:)
private String getRepeatedString(String[] football_club) {
    List<String> List1 = Arrays.asList(football_club);
    HashMap<String, Integer> data = new HashMap<String, Integer>();
    int max = 0;
    for (String string : List1) {
        int count = 1;
        if (data.containsKey(string)) {
            count = data.get(string) + 1;
            data.put(string, count);
        } else {
            data.put(string, 1);
        }
        if (max < count)
            max =count;
    }
    System.out.println(data);
    for (Entry<String, Integer> entry : data.entrySet()) {
        if (max == entry.getValue()) {
            return entry.getKey();
        }
    }
    return null;
}