有没有一种方法可以找到 ArrayList
中最常见的 String
?
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("test");
list.add("hello");
list.add("test");
从这个列表["test","test","hello","test"]
中查找单词"test"
有没有一种方法可以找到 ArrayList
中最常见的 String
?
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("test");
list.add("hello");
list.add("test");
从这个列表["test","test","hello","test"]
中查找单词"test"
不要重复造轮子,使用Collections
类的frequency
方法:
public static int frequency(Collection<?> c, Object o)
返回指定集合中等于指定对象的元素数量。更正式地说,返回集合中满足(o == null ? e == null : o.equals(e))的元素e的数量。
如果您需要计算所有元素的出现次数,请使用Map和巧妙地循环或将列表放入Set并在集合的每个元素上使用上面的frequency方法。HTH
编辑/Java 8: 如果您喜欢具有lambda表达式的更功能性的Java 8单行解决方案,请尝试:Map<String, Long> occurrences =
list.stream().collect(Collectors.groupingBy(w -> w, Collectors.counting()));
Function.identity()
代替w->w
- Cemow -> w
更不啰嗦。有什么支持使用 Function.identity()
的强有力的论据吗? - VH-NZZ在统计学中,这被称为“众数(mode)”。一个标准的Java 8解决方案看起来像这样:
Stream.of("test","test","hello","test")
.collect(Collectors.groupingBy(s -> s, Collectors.counting()))
.entrySet()
.stream()
.max(Comparator.comparing(Entry::getValue))
.ifPresent(System.out::println);
导致结果为:
test=3
jOOλ是一个支持在流上使用mode()
方法的库。以下程序:
System.out.println(
Seq.of("test","test","hello","test")
.mode()
);
产生:
Optional[test]
(免责声明:本人为jOOλ背后公司的员工)
String mostRepeatedWord
= list.stream()
.collect(Collectors.groupingBy(w -> w, Collectors.counting()))
.entrySet()
.stream()
.max(Comparator.comparing(Entry::getValue))
.get()
.getKey();
HashMap<String,Integer>
。如果字符串已经在Map中出现过,则将其 键 的值加一,否则将其添加到Map中。put("someValue", 1);
put("someValue", get("someValue") + 1);
由于“someValue”的键是1,现在当您放置它时,键将变为2。
之后,您可以轻松地遍历映射并提取具有最高价值的键。
我没有编写完整的解决方案,如果您有问题,请在另一个问题中发布。最佳实践是自学。
yourMap.compute("someValue", (k, counter) -> counter == null ? 0 : counter + 1)
向地图中插入数据! - Jacob van LingenMap<String, Integer> stringsCount = new HashMap<>();
遍历你的数组,填充这个映射表:
for(String s: list)
{
Integer c = stringsCount.get(s);
if(c == null) c = new Integer(0);
c++;
stringsCount.put(s,c);
}
Map.Entry<String,Integer> mostRepeated = null;
for(Map.Entry<String, Integer> e: stringsCount.entrySet())
{
if(mostRepeated == null || mostRepeated.getValue()<e.getValue())
mostRepeated = e;
}
并显示最常见的字符串:
if(mostRepeated != null)
System.out.println("Most common string: " + mostRepeated.getKey());
如果有人需要在通常的String[]数组中找到最受欢迎的(使用列表):
public String findPopular (String[] array) {
List<String> list = Arrays.asList(array);
Map<String, Integer> stringsCount = new HashMap<String, Integer>();
for(String string: list)
{
if (string.length() > 0) {
string = string.toLowerCase();
Integer count = stringsCount.get(string);
if(count == null) count = new Integer(0);
count++;
stringsCount.put(string,count);
}
}
Map.Entry<String,Integer> mostRepeated = null;
for(Map.Entry<String, Integer> e: stringsCount.entrySet())
{
if(mostRepeated == null || mostRepeated.getValue()<e.getValue())
mostRepeated = e;
}
try {
return mostRepeated.getKey();
} catch (NullPointerException e) {
System.out.println("Cannot find most popular value at the List. Maybe all strings are empty");
return "";
}
}
ArrayList<String> names = ...
// count names
HashMultiset<String> namesCounts = HashMultiset.create(names);
Set<Multiset.Entry<String>> namesAndCounts = namesCounts.entrySet();
// find one most common
Multiset.Entry<String> maxNameByCount = Collections.max(namesAndCounts, Comparator.comparing(Multiset.Entry::getCount));
// pick all with the same number of occurrences
List<String> mostCommonNames = new ArrayList<>();
for (Multiset.Entry<String> nameAndCount : namesAndCounts) {
if (nameAndCount.getCount() == maxNameByCount.getCount()) {
mostCommonNames.add(nameAndCount.getElement());
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
公共类StringChecker {
public static void main(String[] args) {
ArrayList<String> string;
string = new ArrayList<>(Arrays.asList("Mah", "Bob", "mah", "bat", "MAh", "BOb"));
Map<String, Integer> wordMap = new HashMap<String, Integer>();
for (String st : string) {
String input = st.toUpperCase();
if (wordMap.get(input) != null) {
Integer count = wordMap.get(input) + 1;
wordMap.put(input, count);
} else {
wordMap.put(input, 1);
}
}
System.out.println(wordMap);
Object maxEntry = Collections.max(wordMap.entrySet(), Map.Entry.comparingByValue()).getKey();
System.out.println("maxEntry = " + maxEntry);
}
public static void main(String[] args) {
List <String> words = new ArrayList<>() ;
words.add("cat") ;
words.add("dog") ;
words.add("egg") ;
words.add("chair") ;
words.add("chair") ;
words.add("chair") ;
words.add("dog") ;
words.add("dog") ;
Map<String,Integer> count = new HashMap<>() ;
for (String word : words) { /* Counts the quantity of each
element */
if (! count.containsKey(word)) {
count.put(word, 1 ) ;
}
else {
int value = count.get(word) ;
value++ ;
count.put(word, value) ;
}
}
List <String> mostCommons = new ArrayList<>() ; /* Max elements */
for ( Map.Entry<String,Integer> e : count.entrySet() ) {
if (e.getValue() == Collections.max(count.values() )){
/* The max value of count */
mostCommons.add(e.getKey()) ;
}
}
System.out.println(mostCommons);
}
}
HashMap<String,Integer>
。遍历数组,对于每个 String
,如果它还不是你的 HashMap
的键,则将其添加并将值设置为 1;如果它已经是键,则将其值增加 1。String
和与之关联的数字(表示它们在数组中出现的次数)的 HashMap
。