删除重复元素并计算ArrayList中的重复次数

问题描述 投票:1回答:3

这比我想象的要困难。我有一个排序的字符串ArrayList(单词),我的任务是删除重复并打印出每个单词的列表,然后是单词的重复次数。我只想说它比我想象的要复杂得多。在尝试不同的事情后,我决定使用HashMap来存储单词(键),值(重复)。

这是代码。 Dictionary是HashMap的排序ArrayList和Repetitions。

public void countElements ()
    {
    String word=dictionary.get(0);
            int wordCount=1;
            int count=dictionary.size();
            for (int i=0;i<count;i++)
            {
                word=dictionary.get(i);

                for (int j=i+1; j<count;j++)
                {


                    if(word.equals(dictionary.get(j)))
                    {

                        wordCount=wordCount+1;
                        repetitions.put(word, wordCount);                           
                        dictionary.remove(j--);                            
                        count--;

                    }


                }
            }

由于某种原因我不理解(我是初学者),在我调用dictionary.remove(j--)方法之后,变量j递减1,即使它应该是i + 1。我错过了什么?任何有关如何正确做到这一点的想法将不胜感激。我知道最好使用迭代器,但这会变得更加混乱。非常感谢。

java arraylist hashmap counting
3个回答
0
投票

此代码将满足您的目的。现在字典将包含唯一的单词,而hashmap将包含每个单词的频率计数。

   public class newq {

    public static void main(String[] args)
    {
        ArrayList<String> dictionary=new ArrayList<String>();
        dictionary.add("hello");
        dictionary.add("hello");
        dictionary.add("asd");
        dictionary.add("qwet");
        dictionary.add("qwet");
        HashMap<String,Integer> hs=new HashMap<String,Integer>();
        int i=0;
        while(i<dictionary.size())
        {
            String word=dictionary.get(i);
            if(hs.containsKey(word))   // check if word repeated
            {
               hs.put(word, hs.get(word)+1); //if repeated increase the count
               dictionary.remove(i); // remove the word

            }
            else
            {
               hs.put(word, 1);  //not repeated
               i++;
            }

       }
       Iterator it = hs.entrySet().iterator();
       while(it.hasNext())
        {
          HashMap.Entry pair = (HashMap.Entry)it.next();
           System.out.println(pair.getKey() + " = " + pair.getValue());
          it.remove();
        }
        for(String word: dictionary)
        {
           System.out.println(word);
        }
   }
}

1
投票

使用流的版本:

    final Map<String, Long> countMap = dictionary.stream().collect(
            Collectors.groupingBy(word -> word, LinkedHashMap::new, Collectors.counting()));
    System.out.println("Counts follow");
    System.out.println(countMap);
    System.out.println("Duplicate-free list follows");
    System.out.println(countMap.keySet());

在这里,我们使用每个元素(即每个单词)将列表中的元素(使用Collectors.groupingBy)分组为结果映射中的键,并计算此单词出现次数(使用Collectors.counting())。

外收集器(groupingBy)使用counting收集器作为下游收集器,收集(这里,计数)单个单词的所有出现。

我们在这里使用LinkedHashMap来构建地图,因为它维护了键值对添加到它的顺序,因为我们希望保持单词在初始列表中的顺序。

还有一件事:countMap.keySet()不是List。如果你想最终得到一个List,它只是new ArrayList(countMap.keySet())


0
投票

如果你不想'j'减少你应该使用j-1。使用j--, - j,j ++或++ j可以更改变量的值。

This link有一个很好的解释和关于post-en pre-incrementing的简单例子。

© www.soinside.com 2019 - 2024. All rights reserved.