Java 在ArrayList中查找最常用的字符串
我的问题有点不同。我有一个ArrayList,其中包含一些路由。例如:Java 在ArrayList中查找最常用的字符串,java,string,list,arraylist,hashmap,Java,String,List,Arraylist,Hashmap,我的问题有点不同。我有一个ArrayList,其中包含一些路由。例如: [ACD, CDE, DEB, EBJ, BJK, JKO, ACD, CDE, DEX, EXB, XBJ, BJK, JKO, KOL] 当我使用HashMap进行计数时,它只打印一个字符串: Most common route: ACD This route repeats 2 times. 这是正确的,但是字符串CDE、BJK和JKO也重复2次。由于我是一名编程新手,请您告诉我我的代码中需要更改哪些内容,以便打印
[ACD, CDE, DEB, EBJ, BJK, JKO, ACD, CDE, DEX, EXB, XBJ, BJK, JKO, KOL]
当我使用HashMap进行计数时,它只打印一个字符串:
Most common route: ACD
This route repeats 2 times.
这是正确的,但是字符串CDE、BJK和JKO也重复2次。由于我是一名编程新手,请您告诉我我的代码中需要更改哪些内容,以便打印所有最常见的路由(字符串)。代码如下:
Map<String, Integer> stringsCount = new HashMap<>();
for(String string: listaRuta)
{
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;
}
if(mostRepeated != null)
System.out.println("Most common route: " + mostRepeated.getKey());
System.out.println("This route repeats " + mostRepeated.getValue() + " times.");
Map stringscont=new HashMap();
for(字符串:listaRuta)
{
整数计数=stringscont.get(字符串);
如果(count==null)count=新整数(0);
计数++;
stringscont.put(字符串,计数);
}
Map.Entry mostRepeated=null;
对于(Map.Entry e:stringscont.entrySet())
{
if(mostRepeated==null | | mostRepeated.getValue()package de.ninjaneers;
导入java.util.array;
导入java.util.Map;
导入java.util.Objects;
导入java.util.stream.collector;
公共班机{
公共静态void main(字符串[]args){
字符串[]项={“ABC”、“ABC”、“DEF”、“DEF”、“DD”、“MM”};
映射计数=数组.流(条目)
.collect(Collectors.groupingBy(e->e,Collectors.counting());
Long max=counts.entrySet().stream()
.map(map.Entry::getValue)
.max(长::比较)
.orElse(0升);
仅映射hemax=counts.entrySet().stream()
.filter(e->Objects.equals(max,e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
}
}
首先计算所有条目,其次计算最大值,最后过滤掉所有小于最大值的条目。对不起,这是第一个简短的示例。package de.ninjaneers;
导入java.util.array;
导入java.util.Map;
导入java.util.Objects;
导入java.util.stream.collector;
公共班机{
公共静态void main(字符串[]args){
字符串[]项={“ABC”、“ABC”、“DEF”、“DEF”、“DD”、“MM”};
映射计数=数组.流(条目)
.collect(Collectors.groupingBy(e->e,Collectors.counting());
Long max=counts.entrySet().stream()
.map(map.Entry::getValue)
.max(长::比较)
.orElse(0升);
仅映射hemax=counts.entrySet().stream()
.filter(e->Objects.equals(max,e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
}
}
首先计算所有条目,然后计算最大值,最后过滤掉所有小于最大值的条目。很抱歉第一个简短的示例。没有流和lambda的解决方案(初学者更容易理解):
测试类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Counter {
public static void main(String[] args) {
List<String> test = new ArrayList<>();
test.add("a");
test.add("b");
test.add("b");
test.add("c");
test.add("c");
test.add("c");
Map<String, Integer> stringsCount = new HashMap<>();
for (String s : test) {
if (stringsCount.get(s) == null) {
stringsCount.put(s, 1);
} else {
stringsCount.replace(s, stringsCount.get(s) + 1);
}
}
for (Map.Entry entry : stringsCount.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共课柜台{
公共静态void main(字符串[]args){
列表测试=新建ArrayList();
测试。添加(“a”);
测试。添加(“b”);
测试。添加(“b”);
测试。添加(“c”);
测试。添加(“c”);
测试。添加(“c”);
Map stringscont=new HashMap();
用于(字符串s:测试){
if(stringscont.get)=null{
stringsCount.put(s,1);
}否则{
StringScont.replace(s,StringScont.get(s)+1);
}
}
对于(Map.Entry:stringscont.entrySet()){
System.out.println(entry.getKey()+”,“+entry.getValue());
}
}
}
输出
a、 一,
b、 二,
c、 3没有流和lambda的解决方案(初学者更容易理解):
测试类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Counter {
public static void main(String[] args) {
List<String> test = new ArrayList<>();
test.add("a");
test.add("b");
test.add("b");
test.add("c");
test.add("c");
test.add("c");
Map<String, Integer> stringsCount = new HashMap<>();
for (String s : test) {
if (stringsCount.get(s) == null) {
stringsCount.put(s, 1);
} else {
stringsCount.replace(s, stringsCount.get(s) + 1);
}
}
for (Map.Entry entry : stringsCount.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共课柜台{
公共静态void main(字符串[]args){
列表测试=新建ArrayList();
测试。添加(“a”);
测试。添加(“b”);
测试。添加(“b”);
测试。添加(“c”);
测试。添加(“c”);
测试。添加(“c”);
Map stringscont=new HashMap();
用于(字符串s:测试){
if(stringscont.get)=null{
stringsCount.put(s,1);
}否则{
StringScont.replace(s,StringScont.get(s)+1);
}
}
对于(Map.Entry:stringscont.entrySet()){
System.out.println(entry.getKey()+”,“+entry.getValue());
}
}
}
输出
a、 一,
b、 二,
c、 3此外,我更喜欢使用lamba解决方案来构建hasmap。一旦您拥有了该地图,这是您的目标的常用方法:
int maxValue=(Collections.max(stringsCount.values())); // This will return max value in the Hashmap
for (Entry<String, Integer> entry : stringsCount.entrySet()) { // Iterate through the hashmap
if (entry.getValue()==maxValue) {
System.out.println(entry.getKey()); // Print the key with max value
}
}
此外,我更喜欢使用lamba解决方案来构建hasmap。一旦您拥有了该映射,这是您的目标的常见用途:
int maxValue=(Collections.max(stringsCount.values())); // This will return max value in the Hashmap
for (Entry<String, Integer> entry : stringsCount.entrySet()) { // Iterate through the hashmap
if (entry.getValue()==maxValue) {
System.out.println(entry.getKey()); // Print the key with max value
}
}
这里有一个使用包
和topOccurrences
的解决方案
Bag
类似于Map
,其中的值是键的计数。从外部看,Bag
的接口类似于Collection
。Eclipse集合具有原始集合,因此HashBag实现在内部使用ObjectIntHashMap
来提高效率
Bag
上的方法topOccurrences
具有处理关系的逻辑,这就是为什么在结果列表中返回多个ObjectIntPair
注意:我是Eclipse集合的提交者。这里有一个使用包和的解决方案
Bag
类似于Map
,其中的值是键的计数。从外部看,Bag
的接口类似于Collection
。Eclipse集合具有原始集合,因此HashBag实现在内部使用ObjectIntHashMap
来提高效率
Bag
上的方法topOccurrences
具有处理关系的逻辑,这就是为什么在结果列表中返回多个ObjectIntPair
注:我是一名委员
MutableList<String> list =
Lists.mutable.with("ACD", "CDE", "DEB", "EBJ", "BJK", "JKO",
"ACD", "CDE", "DEX", "EXB", "XBJ", "BJK", "JKO", "KOL");
MutableList<ObjectIntPair<String>> pairs =
list.toBag().topOccurrences(1);
System.out.println(pairs);
[BJK:2, JKO:2, ACD:2, CDE:2]