从阵列
获取最频繁的值I有如下所示从阵列
String[] football_club = {"Barcelona", "Real Madrid", "Chelsea", "Real Madrid", "Barcelona", "Barcelona"};
//result
Sting result_club = "a value most in the array"
从上面的阵列的阵列,其具有通常在阵列中存在的值“巴塞罗那”。
编码如何查找数组中出现频率最高的值?
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());
}
}
稍后,您可以使用比较器对项目进行排序地图由amount
如下写道(这只是一个例子,改变自我的教育方式:
public class Testing {
public static void main(String[] args) {
HashMap<String,Double> map = new HashMap<String,Double>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);
map.put("A",99.5);
map.put("B",67.4);
map.put("C",67.4);
map.put("D",67.3);
System.out.println("unsorted map: "+map);
sorted_map.putAll(map);
System.out.println("results: "+sorted_map);
}
}
class ValueComparator implements Comparator<String> {
Map<String, Double> base;
public ValueComparator(Map<String, Double> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with equals.
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}
那么当你整理你的HashMap中,只得到第一个关键,这将是最频繁的关键
您可以制作一个HashMap<String,Integer>
。如果字符串已经出现在地图中,请将其键加1,否则将其添加到地图中。
例如:
put("Barcelona", 1);
然后,假定它的 “巴塞罗那” 同样,你可以这样做:
put("Barcelona", get("Barcelona") + 1);
由于 “巴塞罗那” 的关键是1,现在当你把它的关键将是2.
您可以定义一个封装了HashMap对象的新类。
然后你可以定义你自己的方法并获得方法。
另一种方式:对数组进行排序,然后迭代它以统计连续的公共元素,并保持最大计数/对应元素的轨迹。
的排序可能是重(相对的),但我认为HashMap的访问是复杂的相同幅度(O(n.log(N))的
+1替代解决方案。 – Maroun
如果性能并不重要:
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.");
}
这很好用,max是用来存储最大计数的新变量,如果两个元素的频率相同String [] football_club = {“Chelsea”,“Chelsea”,“Real Madrid”,“Real Madrid”,“Real Madrid” ,“巴塞罗那”,“切尔西”};
This Returns: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;
}
帮我理解你的麻烦。你想计算一个单词在你的数组中重复的次数吗? – Blackbelt
请查看。希望它能帮助你。 http://fruzenshtein.com/exercise-1/ –
你应该提供更多的细节[你尝试过](http://mattgemmell.com/2008/12/08/what-have-you-tried/)以及你遇到问题的地方。只是要求解决问题的代码可能会被视为关于*的主题(请参见[关于提问的帮助页](http://*.com/help/on-topic))。 –