Java:检查字符串是否在单词表中的最有效方法 [英] Java: Most efficient way to check if a String is in a wordlist

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

问题描述

我有一个字符串数组 String[] words 和一个 28000 字的词表.

I have an array of strings String[] words and a 28000 word Word-list.

我想检查字符串数组的任何成员是否在 WordList 中(单词列表在文本文件 wordlist.txt 中)

I want to check if any member of the String array is in the WordList (the word-list is in a text file wordlist.txt)

最有效的方法是什么?

推荐答案

将字符串直接放入 HashSet 而不是数组,并使用 包含 上设置检查内容.您不会改进 O(1) 访问.如果存在任何重复项,这也将最大限度地减少用于存储 Strings 的内存.

Place the strings directly into a HashSet<String> rather than an array and iterate through the file using contains on the set to check the content. You wont improve on O(1) access. This will also mimimize memory used to store the Strings should any duplicates exist.

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

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