HashMaplt;String, Integergt; Search for part of an key?(HashMap字符串,整数搜索密钥的一部分?)
问题描述
我目前正在使用 HashMap<String, Integer>
,其中填充了 String
类型的键,例如 5 个字符长.如何搜索 4 个字符或更少字符的特定键,该键是其他键的一部分和开头,并将所有命中作为 <Key, Value>
的集合?
I am currently using HashMap<String, Integer>
which is filled with keys of type String
which are all, let's say, 5 chars long. How can I search for an specific key of 4 chars or less, which is part and at the beginning of some other keys and get all hits as a collection of <Key, Value>
?
推荐答案
迭代是您唯一的选择,除非您创建自定义数据结构:
Iterate is your only option unless you create a custom data structure:
for (Entry<String, Integer> e : map.entrySet()) {
if (e.getKey().startsWith("xxxx")) {
//add to my result list
}
}
如果您需要更节省时间的东西,那么您需要一个用于跟踪这些部分键的 map 实现.
If you need something more time efficient then you'd need an implementation of map where you are tracking these partial keys.
这篇关于HashMap<字符串,整数>搜索密钥的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HashMap<字符串,整数>搜索密钥的一部分?


基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01