Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中搜索集合_Java_Search_String_Collections - Fatal编程技术网

在Java中搜索集合

在Java中搜索集合,java,search,string,collections,Java,Search,String,Collections,我有一个java属性文件,其中包含国家名称和代码的键/值对。我将把这个文件的内容加载到一个集合中,比如List或HashMap 然后,我希望用户能够搜索国家,例如,如果他们在文本框中键入“Aus”,然后单击提交,那么我希望搜索我拥有的集合,其中包含国家代码/名称的键/值对(例如Aus=>Australia),并返回找到匹配的国家 除了循环遍历集合的元素并使用charAt()?之外,还有其他更有效的方法来执行此操作吗?除了通过以下方式对集合进行索引之外,您还必须通过循环遍历所有元素来手动检查。您可

我有一个java属性文件,其中包含国家名称和代码的键/值对。我将把这个文件的内容加载到一个集合中,比如List或HashMap

然后,我希望用户能够搜索国家,例如,如果他们在文本框中键入“
Aus
”,然后单击提交,那么我希望搜索我拥有的集合,其中包含国家代码/名称的键/值对(例如Aus=>Australia),并返回找到匹配的国家


除了循环遍历集合的元素并使用
charAt()

之外,还有其他更有效的方法来执行此操作吗?

除了通过以下方式对集合进行索引之外,您还必须通过循环遍历所有元素来手动检查。您可以使用
startsWith
,而不是在字符串上循环:

String userText = ...
for (Map.Entry<String, String> entry : map) {
    boolean entryMatches = entry.getKey().startsWith(userText);
    ...
String userText=。。。
for(Map.Entry:Map){
boolean entryMatches=entry.getKey().startsWith(userText);
...
或者使用正则表达式:

Pattern pattern = Pattern.compile(userText);

for (Map.Entry<String, String> entry : map) {
    boolean entryMatches = pattern.matcher(entry.getKey()).find();
    ...
Pattern=Pattern.compile(userText);
for(Map.Entry:Map){
boolean entryMatches=pattern.matcher(entry.getKey()).find();
...

除了通过以下方式为集合编制索引外,您还必须通过循环所有元素来手动检查。您可以使用
startsWith
,而不是在字符串上循环:

String userText = ...
for (Map.Entry<String, String> entry : map) {
    boolean entryMatches = entry.getKey().startsWith(userText);
    ...
String userText=。。。
for(Map.Entry:Map){
boolean entryMatches=entry.getKey().startsWith(userText);
...
或者使用正则表达式:

Pattern pattern = Pattern.compile(userText);

for (Map.Entry<String, String> entry : map) {
    boolean entryMatches = pattern.matcher(entry.getKey()).find();
    ...
Pattern=Pattern.compile(userText);
for(Map.Entry:Map){
boolean entryMatches=pattern.matcher(entry.getKey()).find();
...

使用String.contains()循环是一种方法,除非你想在像这样的重炮中移动。

使用String.contains()循环除非你想在重炮中移动,否则就是这样。

因为列表小到可以加载到内存中,所以对它进行排序,然后使用静态方法java.util.Collections.binarySearch(),进行二进制搜索。这将返回一个索引,并且无论确切的字符串是否在列表中都有效(虽然如果不是,它返回一个负数,所以一定要检查)。然后,从该索引开始,只需向前迭代以查找所有带有该前缀的字符串。作为一个好的副作用,结果输出将按字母顺序排列


要使整个过程不区分大小写,请记住在加载列表时将其转换为小写,当然,在搜索之前将前缀转换为小写。

由于列表足够小,可以加载到内存中,请对其进行排序,然后使用静态方法java.util.Collections.binarySearch()进行二进制搜索。这将返回一个索引,并且无论确切的字符串是否在列表中都有效(尽管如果不是,则返回一个负数,因此请务必检查)。然后,从该索引开始,只需向前迭代以查找具有该前缀的所有字符串。作为一个很好的副作用,结果输出将按字母顺序排列


要使整个过程不区分大小写,请记住在加载列表时将其转换为小写,当然,在搜索之前将前缀转换为小写。

如果性能很重要,可以使用树集或树映射保存国家名称,并执行以下操作,以识别以给定字符串开头的国家

NavigableMap<String, String> countries = new TreeMap<String, String>();
countries.put("australia", "Australia");
...

String userText = ...
String tmp = userText.toLower();
List<String> hits = new ArrayList<String>();
Map.Entry<String, String> entry = countries.ceilingEntry(tmp);
while (entry != null && entry.getKey().startsWith(tmp)) {
    hits.add(entry.getValue());
    entry = map.higherEntry(entry.getKey());
}
// hits now contains all country names starting with the value of `userText`, 
// ignoring differences in letter case.
NavigableMap countries=newtreemap();
国家。put(“澳大利亚”、“澳大利亚”);
...
字符串userText=。。。
字符串tmp=userText.toLower();
列表点击次数=新建ArrayList();
Map.Entry=countries.ceilingEntry(tmp);
while(entry!=null&&entry.getKey().startsWith(tmp)){
hits.add(entry.getValue());
entry=map.higherEntry(entry.getKey());
}
//hits现在包含所有以“userText”值开头的国家名称,
//忽略字母大小写的差异。

这是
O(logN)
,其中N是国家数量。相比之下,对集合的线性搜索是
O(N)

如果性能很重要,可以使用树集或树映射保存国家名称,并执行以下操作可用于识别以给定字符串开头的国家

NavigableMap<String, String> countries = new TreeMap<String, String>();
countries.put("australia", "Australia");
...

String userText = ...
String tmp = userText.toLower();
List<String> hits = new ArrayList<String>();
Map.Entry<String, String> entry = countries.ceilingEntry(tmp);
while (entry != null && entry.getKey().startsWith(tmp)) {
    hits.add(entry.getValue());
    entry = map.higherEntry(entry.getKey());
}
// hits now contains all country names starting with the value of `userText`, 
// ignoring differences in letter case.
NavigableMap countries=newtreemap();
国家。put(“澳大利亚”、“澳大利亚”);
...
字符串userText=。。。
字符串tmp=userText.toLower();
列表点击次数=新建ArrayList();
Map.Entry=countries.ceilingEntry(tmp);
while(entry!=null&&entry.getKey().startsWith(tmp)){
hits.add(entry.getValue());
entry=map.higherEntry(entry.getKey());
}
//hits现在包含所有以“userText”值开头的国家名称,
//忽略字母大小写的差异。

这是
O(logN)
其中N是国家数。相比之下,对集合的线性搜索是
O(N)

这简直是胡说八道!你不能对“澳大利亚”进行
binarySearch
,然后找到“澳大利亚”显然,在任何java集合中,这都是假设列表只加载和排序一次,而不是每次请求都加载和排序!@oxbox\u lakes:我解释了如何使其不区分大小写。我正在搜索“aus”以查找“australia”@Todd-这仍然是胡说八道。你不能像你说的那样在集合中没有精确字符串的情况下进行二进制搜索。如果你认为我错了,请修改你的答案以包含一个工作示例。只需尝试
Collections.binarySearch(singletonList(“australia”),“aus”)
你会发现我是right@oxbow_lakes:如果有人投票反对我,我将提供一个完整的示例。但首先,我让您参考API文档,其中说明此方法返回“搜索键的索引,如果它包含在列表中;否则,((插入点)-1)。插入点定义为将键插入列表的点:第一个元素的索引大于键”。因此,如果返回值为x且x<0,则为1