Java 如何计算数组列表中单词的重复次数?

Java 如何计算数组列表中单词的重复次数?,java,arraylist,count,counter,Java,Arraylist,Count,Counter,我有这些代码来搜索数组列表中的匹配项,但我的问题是如何得到结果 整数类型的for循环的out-side,因为我需要in-out-side,可能还有另一种查找方法 没有使用for循环的事件你能帮我吗? 谢谢你 List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("aaa"); Set<String> unique = new HashSe

我有这些代码来搜索数组列表中的匹配项,但我的问题是如何得到结果 整数类型的for循环的out-side,因为我需要in-out-side,可能还有另一种查找方法 没有使用for循环的事件你能帮我吗? 谢谢你

List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");

Set<String> unique = new HashSet<String>(list);
for (String key : unique) {
 int accurNO = Collections.frequency(list, key);
    System.out.println(key + ": " accurNO);
}
List List=new ArrayList();
列表。添加(“aaa”);
列表。添加(“bbb”);
列表。添加(“aaa”);
Set unique=新哈希集(列表);
for(字符串键:唯一){
int accurNO=集合频率(列表、键);
System.out.println(键+“:”accurNO);
}

您应该声明一个类似于
map countMap=new HashMap()的映射并在循环中填充它

Map<String, Integer> countMap = new HashMap<String, Integer>();
for (String key : unique) {
    int accurNO = Collections.frequency(list, key);
    coutMap.put(key, accurNO);
    //...
}
//now you have a map with keys and their frequencies in the list
Map countMap=newhashmap();
for(字符串键:唯一){
int accurNO=集合频率(列表、键);
coutMap.put(键,accurNO);
//...
}
//现在,列表中有了一个带有键及其频率的地图

您应该声明一个类似于
map countMap=new HashMap()的映射并在循环中填充它

Map<String, Integer> countMap = new HashMap<String, Integer>();
for (String key : unique) {
    int accurNO = Collections.frequency(list, key);
    coutMap.put(key, accurNO);
    //...
}
//now you have a map with keys and their frequencies in the list
Map countMap=newhashmap();
for(字符串键:唯一){
int accurNO=集合频率(列表、键);
coutMap.put(键,accurNO);
//...
}
//现在,列表中有了一个带有键及其频率的地图
List List=new ArrayList();
列表。添加(“aaa”);
列表。添加(“bbb”);
列表。添加(“aaa”);
Map countMap=新的HashMap();
Set unique=新哈希集(列表);
for(字符串键:唯一){
int accurNO=集合频率(列表、键);
countMap.put(键,accurNO);
System.out.println(键+“:”accurNO);
}
List List=new ArrayList();
列表。添加(“aaa”);
列表。添加(“bbb”);
列表。添加(“aaa”);
Map countMap=新的HashMap();
Set unique=新哈希集(列表);
for(字符串键:唯一){
int accurNO=集合频率(列表、键);
countMap.put(键,accurNO);
System.out.println(键+“:”accurNO);
}
Set unique=新哈希集(列表)

收集。频率(列表、键)

你的开销太大了

我会这样做的

List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");

Map<String, Integer> countMap = new HashMap<>();


for (String word : list) {
    Integer count = countMap.get(word);
    if(count == null) {
        count = 0;
    }
    countMap.put(word, (count.intValue()+1));
}

System.out.println(countMap.toString());
编辑逐个输出:迭代映射的一组条目

for(Entry<String, Integer> entry : countMap.entrySet()) {
    System.out.println("frequency of '" + entry.getKey() + "' is "
          + entry.getValue());
}
编辑2无需循环

String word = null;
Integer frequency = null;

word = "aaa";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " +
    (frequency == null ? 0 : frequency.intValue()));

word = "bbb";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " + 
    (frequency == null ? 0 : frequency.intValue()));

word = "foo";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " + 
    (frequency == null ? 0 : frequency.intValue()));
输出

{aaa=2, bbb=1}
frequency of 'aaa' is 2
frequency of 'bbb' is 1
frequency of 'aaa' is 2
frequency of 'bbb' is 1
frequency of 'foo' is 0
请注意,您将始终拥有一个集合,您需要以某种方式从集合中提取特定单词的计数

Set unique=新哈希集(列表)

收集。频率(列表、键)

你的开销太大了

我会这样做的

List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");

Map<String, Integer> countMap = new HashMap<>();


for (String word : list) {
    Integer count = countMap.get(word);
    if(count == null) {
        count = 0;
    }
    countMap.put(word, (count.intValue()+1));
}

System.out.println(countMap.toString());
编辑逐个输出:迭代映射的一组条目

for(Entry<String, Integer> entry : countMap.entrySet()) {
    System.out.println("frequency of '" + entry.getKey() + "' is "
          + entry.getValue());
}
编辑2无需循环

String word = null;
Integer frequency = null;

word = "aaa";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " +
    (frequency == null ? 0 : frequency.intValue()));

word = "bbb";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " + 
    (frequency == null ? 0 : frequency.intValue()));

word = "foo";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " + 
    (frequency == null ? 0 : frequency.intValue()));
输出

{aaa=2, bbb=1}
frequency of 'aaa' is 2
frequency of 'bbb' is 1
frequency of 'aaa' is 2
frequency of 'bbb' is 1
frequency of 'foo' is 0

请注意,您将始终拥有一个集合,并且您需要以某种方式从集合中提取特定单词的计数。

地图答案有效,但您可以扩展此答案以解决更多问题

创建一个具有所需字段值的类,并将该类放入列表中

import java.util.ArrayList;
import java.util.List;

public class WordCount {

    private String word;
    private int count;

    public WordCount(String word) {
        this.word = word;
        this.count = 0;
    }

    public void addCount() {
        this.count++;
    }

    public String getWord() {
        return word;
    }

    public int getCount() {
        return count;
    }

}

class AccumulateWords {
    List<WordCount> list    = new ArrayList<WordCount>();

    public void run() {
        list.add(new WordCount("aaa"));
        list.add(new WordCount("bbb"));
        list.add(new WordCount("ccc"));

        // Check for word occurrences here

        for (WordCount wordCount : list) {
            int accurNO = wordCount.getCount();
            System.out.println(wordCount.getWord() + ": " + accurNO);
        }
    }
}
import java.util.ArrayList;
导入java.util.List;
公共类字数{
私有字符串字;
私人整数计数;
公共字计数(字符串字){
这个单词=单词;
此值为0.count;
}
公共void addCount(){
这个.count++;
}
公共字符串getWord(){
返回词;
}
public int getCount(){
返回计数;
}
}
类累积数{
列表=新的ArrayList();
公开募捐{
添加(新字数(“aaa”);
添加(新字数(“bbb”);
添加(新字数(“ccc”);
//在此处检查单词出现情况
用于(字数:列表){
int accurNO=wordCount.getCount();
System.out.println(wordCount.getWord()+“:”+accurNO);
}
}
}

地图答案有效,但您可以扩展此答案以解决更多问题

创建一个具有所需字段值的类,并将该类放入列表中

import java.util.ArrayList;
import java.util.List;

public class WordCount {

    private String word;
    private int count;

    public WordCount(String word) {
        this.word = word;
        this.count = 0;
    }

    public void addCount() {
        this.count++;
    }

    public String getWord() {
        return word;
    }

    public int getCount() {
        return count;
    }

}

class AccumulateWords {
    List<WordCount> list    = new ArrayList<WordCount>();

    public void run() {
        list.add(new WordCount("aaa"));
        list.add(new WordCount("bbb"));
        list.add(new WordCount("ccc"));

        // Check for word occurrences here

        for (WordCount wordCount : list) {
            int accurNO = wordCount.getCount();
            System.out.println(wordCount.getWord() + ": " + accurNO);
        }
    }
}
import java.util.ArrayList;
导入java.util.List;
公共类字数{
私有字符串字;
私人整数计数;
公共字计数(字符串字){
这个单词=单词;
此值为0.count;
}
公共void addCount(){
这个.count++;
}
公共字符串getWord(){
返回词;
}
public int getCount(){
返回计数;
}
}
类累积数{
列表=新的ArrayList();
公开募捐{
添加(新字数(“aaa”);
添加(新字数(“bbb”);
添加(新字数(“ccc”);
//在此处检查单词出现情况
用于(字数:列表){
int accurNO=wordCount.getCount();
System.out.println(wordCount.getWord()+“:”+accurNO);
}
}
}

我会先对列表进行排序,以避免每次都用Collections.frequency遍历整个列表。代码将更长,但效率更高

    List<String> list = new ArrayList<String>();
    list.add("aaa");
    list.add("bbb");
    list.add("aaa");
    Map<String, Integer> map = new HashMap<String, Integer>();
    Collections.sort(list);
    String last = null;
    int n = 0;
    for (String w : list) {
        if (w.equals(last)) {
            n++;
        } else {
            if (last != null) {
                map.put(last, n);
            }
            last = w;
            n = 1;
        }
    }
    map.put(last, n);
    System.out.println(map);

我会先对列表进行排序,以避免每次都用Collections.frequency遍历整个列表。代码将更长,但效率更高

    List<String> list = new ArrayList<String>();
    list.add("aaa");
    list.add("bbb");
    list.add("aaa");
    Map<String, Integer> map = new HashMap<String, Integer>();
    Collections.sort(list);
    String last = null;
    int n = 0;
    for (String w : list) {
        if (w.equals(last)) {
            n++;
        } else {
            if (last != null) {
                map.put(last, n);
            }
            last = w;
            n = 1;
        }
    }
    map.put(last, n);
    System.out.println(map);

也许一个临时变量可能是一个临时变量这是真的,但是你能一个一个地单独得到吗?不像mapdear A4L,第二个代码没问题,但是我怎样才能得到这些entry.getkey()&entry.getValue()呢loop@Freeman你对这些词的使用情况和数量是什么?对于循环,您不需要这样做,它的唯一目的是显示它是有效的。如果你有一个特定的单词,你可以问
地图
它的频率。请看我的第二次编辑。这是真的,但你能一个接一个地单独获取吗?不像mapdear A4L,第二次代码没问题,但我如何才能获取这些entry.getkey()&entry.getValue()loop@Freeman你对这些词的使用情况和数量是什么?对于循环,您不需要这样做,它的唯一目的是显示它是有效的。如果你有一个特定的词,你可以