我在一个文件中管道并将其存储到树中。我正在计数独特的词.. 我把我不想成为一个哈希集的单词。我想要检查文件是否包含这些单词,然后我将它们放置到树集 .. 我知道我需要某种类型的if(word = = find)?我只是不知道该怎么做..在hashset或treeset中查找单词?
抱歉格式。粘贴后很难使其正确。
这就是我..
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...");
}
}
您可能想要阅读TreeSet文档http://java.sun.com/javase/6/docs/api/java/util/TreeSet.html – 2010-01-11 02:15:37