有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在hashset或treeset中查找单词?

我在一个文件中添加管道并将其存储到树集。我试图数一数独特的单词。。 我正在把我不想要的单词放入哈希集中。“a”,“the”,“and”

我想在把这些单词放入文件之前,先检查一下文件中是否包含这些单词 树丛。。 我知道我需要某种if(word==find)?我就是不知道怎么做

抱歉格式化了。粘贴后很难纠正

这就是我的

import java.util.Scanner;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.HashSet;

public class Project1
{
    public static void main(String[] args)
    {
        Scanner     sc = new Scanner(System.in);    
        String      word;
        String grab;
        int count = 0;
        int count2 =0;
        int count3 =0;
        int count4 =0;
        int number;
        TreeSet<String> a = new TreeSet<String>();
        HashSet<String> find = new HashSet<String>();

        System.out.println("Project 1\n");
        find.add("a");
        find.add("and");
        find.add("the");

        while (sc.hasNext()) 
        {   
            word = sc.next();
            word = word.toLowerCase();
            for(int i = 0; i < word.length(); i++ )
            {
                if(Character.isDigit(word.charAt(i))) 
                {
                    count3++;  
                }
            }
            //if( a.contains("a") )
            //|| word.matches("and") || word.matches("the")|| word.contains("$"))
            //{
            //   count2++;
            // }
            a.add(word);
            if (word.equals("---"))
            {
                break;
            }
        }

        System.out.println("a size");
        System.out.println(a.size());
        // count = count2 - count;
        System.out.println("unique words");
        System.out.println(a.size() -  count2 - count3);
        System.out.println("\nbye...");
    }
}

共 (0) 个答案