Java 从字符串数组中删除重复的单词

Java 从字符串数组中删除重复的单词,java,arrays,text,Java,Arrays,Text,早上好 我编写了一个函数,用于计算一个术语的频率: public static int tfCalculator(String[] totalterms, String termToCheck) { int count = 0; //to count the overall occurrence of the term termToCheck for (String s : totalterms) { if (s.equalsIgnoreCase(termToC

早上好

我编写了一个函数,用于计算一个术语的频率:

public static int tfCalculator(String[] totalterms, String termToCheck) {
    int count = 0;  //to count the overall occurrence of the term termToCheck
    for (String s : totalterms) {
        if (s.equalsIgnoreCase(termToCheck)) {
            count++; 
        }
    } 
    return count;
}
然后我在下面的代码中使用它来计算
String[]单词中的每个单词

for(String word:words){
    int freq = tfCalculator(words, word);

    System.out.println(word + "|" + freq);
    mm+=word + "|" + freq+"\n";
}
我的问题是,这里重复的单词是,例如,结果:

  • 细胞骨架| 2
  • 网络| 1
  • 启用| 1
  • 等于| 1
  • 主轴| 1
  • 细胞骨架| 2
那么有人能帮我去掉重复的单词并得到这样的结果吗:

  • 细胞骨架| 2
  • 网络| 1
  • 启用| 1
  • 等于| 1
  • 主轴| 1
多谢各位

您可以只使用一个,它应该能够解决重复的问题:

words = new HashSet<String>(Arrays.asList(words)).toArray(new String[0]);
words=newhashset(Arrays.asList(words)).toArray(新字符串[0]);
这将获取您的数组,将其转换为
列表
,将其提供给
哈希集
的构造函数,然后为您将其转换回数组。

words = Arrays.stream(words).distinct().toArray(String[]::new);

distinct
方法删除重复项<代码>单词
替换为不重复的新数组

对数组排序,然后您可以只计算相等的相邻元素:

Arrays.sort(totalterms);
int i = 0;
while (i < totalterms.length) {
  int start = i;
  while (i < totalterms.length && totalterms[i].equals(totalterms[start])) {
    ++i;
  }
  System.out.println(totalterms[start] + "|" + (i - start));
}
Arrays.sort(totalterms);
int i=0;
而(i
我想这里您需要打印数组totalterms中每个字符串的频率。我认为使用Map是一个更容易的解决方案,因为在数组的单个遍历中,它将存储所有字符串的频率,并检查以下实现

public static void printFrequency(String[] totalterms)
{
    Map frequencyMap = new HashMap<String, Integer>();

    for (String string : totalterms) {
        if(frequencyMap.containsKey(string))
        {
            Integer count = (Integer)frequencyMap.get(string);
            frequencyMap.put(string, count+1);
        }
        else
        {
            frequencyMap.put(string, 1);
        }
    }

    Set <Entry<String, Integer>> elements= frequencyMap.entrySet();

    for (Entry<String, Integer> entry : elements) {
        System.out.println(entry.getKey()+"|"+entry.getValue());
    }
}
公共静态无效打印频率(字符串[]totalterms)
{
Map frequencyMap=new HashMap();
for(字符串:totalterms){
if(frequencyMap.containsKey(字符串))
{
整数计数=(整数)frequencyMap.get(字符串);
frequencyMap.put(字符串,计数+1);
}
其他的
{
frequencyMap.put(字符串,1);
}
}
Set elements=frequencyMap.entrySet();
用于(条目:元素){
System.out.println(entry.getKey()+“|”+entry.getValue());
}
}
分两行:


字符串s=“细胞骨架| 2-网络| 1-启用| 1-相等| 1-纺锤体| 1-细胞骨架| 2”; System.out.println(新的LinkedHashSet(Arrays.asList(s.split(“-”))).toString().replaceAll((^\[\]$),”).replace(“,”,“-”));

您的代码很好,只需跟踪已经遇到的单词。为此,您可以保留一个运行集:

Set<String> prevWords = new HashSet<>();
for(String word:words){
    // proceed if word is new to the set, otherwise skip
    if (prevWords.add(word)) {
        int freq = tfCalculator(words, word);

        System.out.println(word + "|" + freq);
        mm+=word + "|" + freq+"\n";
    }
}
Set prevWords=new HashSet();
for(字符串字:字){
//如果word对集合是新的,则继续,否则跳过
如果(前置词。添加(词)){
int freq=tfCalculator(字,字);
System.out.println(word+“|”+freq);
mm+=word+“|”+freq+“\n”;
}
}

将数组放入一个
集合
中,重复项将被删除gone@KevinEsche但计算频率并不好。我会使用
映射
将单词映射到它们的频率。再说一次,有很多更好的方法来计算频率本身,但那是另一回事。你能发布完整的程序吗?关于代码质量的旁注:一些变量(如mm)或方法的名称。。。情况很糟。如果你给你的方法起了一个名字,说明它真正的作用;事情变得更清楚了。比如:
int countoccurrencesofterm(String term,String[]stringsToCheck)
或者类似的东西。@Mena谢谢你的回答,你能解释一下我如何使用
Map
,或者一种计算术语频率的方法吗谢谢你@Saurav这对我来说很好,我真的很感激这是非常冗长和不必要的。。。最好检查其他一些解决方案@HaKiM's
Set<String> prevWords = new HashSet<>();
for(String word:words){
    // proceed if word is new to the set, otherwise skip
    if (prevWords.add(word)) {
        int freq = tfCalculator(words, word);

        System.out.println(word + "|" + freq);
        mm+=word + "|" + freq+"\n";
    }
}