Java:检查字符串是否在字典中 [英] Java: Check if a string is in a dictionary

查看:534
本文介绍了Java:检查字符串是否在字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个语言字典(即英语,意大利语等),这本质上是每一行都有一个单词的文件。

I have a language dictionary (i.e. english,italian,etc...), that essentially is a file with one word on every line.

现在我想创建一个类,其方法是在输入中给出字符串检查该字符串是否存在于该字典中。

Now i want to create a class with a method that given a string in input check if that string exists into that dictionary.

我的想法是该方法返回一个布尔值。在伪代码中:

My idea is that the method return a boolean value. In pseudocode:

boolean checkWord(String s){
    if(StringIsInDictionary) return true;
    return false
}

应该是实现该功能的最佳方法?

What should be the best way to implement that feature?

考虑到该文件将包含约65000个单词。

Consider that the file will contain ~65000 words.

推荐答案

将字典读入 Set< String> (例如, HashSet< String> ),然后使用 set.contains(word)

Read the dictionary into a Set<String> (for example, HashSet<String>), and then use set.contains(word).

这篇关于Java:检查字符串是否在字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆