英文:
Java HashMap key fits to a pattern?
问题
我有一个Map数据集,我想要遍历键并搜索匹配项。因此,我想要找到键与此模式匹配的映射元素:
String searchedKey = "A?C"; // ?代表可以是任何字符
Map<String, MyObject> myMap = new HashMap<>();
myMap.put("ABC", MyObject(1));
myMap.put("CDF", MyObject(2));
myMap.put("ADS", MyObject(3));
for (Map.Entry<String, MyObject> entry : myMap.entrySet()) {
// 在这种情况下,我想要找到第一个元素,因为它的键符合searchedKey,其中?可以是任何字符
}
我应该如何做到这一点?
谢谢!
英文:
I have a Map dataset, and I want to iterate through the keys and search for matches.
So I want to find the maps element, where the key fits to this pattern:
String searchedKey = "A?C"; // ? means it can be any character
Map<String, MyObject> myMap = new HashMap<>();
myMap.put("ABC", MyObject(1));
myMap.put("CDF", MyObject(2));
myMap.put("ADS", MyObject(3));
for (Map.Entry<String,MyObject> entry : myMap.entrySet()) {
// in this case, I want to find the first element, because it's key fits the searchedKey, where ? can be anything
}
How can I do this?
Thanks!
答案1
得分: 2
你可以像这样做,以返回一个找到的 MyObjects 列表。注意,我将 ?
更改为 .
以匹配任意字符。
String searchedKey = "A.C"; // ? 表示可以是任意字符
Map<String, MyObject> myMap = new HashMap<>();
myMap.put("ABC", new MyObject(1));
myMap.put("CDF", new MyObject(2));
myMap.put("ARS", new MyObject(3));
myMap.put("VS", new MyObject(4));
myMap.put("AQC", new MyObject(3));
myMap.put("DS", new MyObject(3));
myMap.put("ASC", new MyObject(10));
List<Map.Entry<String, MyObject>> list = myMap.entrySet().stream()
.filter(e -> e.getKey().matches(searchedKey))
.collect(Collectors.toList());
list.forEach(System.out::println);
输出结果
ASC=10
ABC=1
AQC=3
MyObject 类
class MyObject {
int val;
public MyObject(int v) {
this.val = v;
}
public String toString() {
return val + "";
}
}
英文:
You could do something like this to return a list of found MyObjects. Note I changed ?
to .
for any character.
String searchedKey = "A.C"; // ? means it can be any character
Map<String, MyObject> myMap = new HashMap<>();
myMap.put("ABC", new MyObject(1));
myMap.put("CDF", new MyObject(2));
myMap.put("ARS", new MyObject(3));
myMap.put("VS", new MyObject(4));
myMap.put("AQC", new MyObject(3));
myMap.put("DS", new MyObject(3));
myMap.put("ASC", new MyObject(10));
List<Map.Entry<String,MyObject>> list = myMap.entrySet().stream()
.filter(e -> e.getKey().matches(searchedKey))
.collect(Collectors.toList());
list.forEach(System.out::println);
Prints
ASC=10
ABC=1
AQC=3
The MyObject class
class MyObject {
int val;
public MyObject(int v) {
this.val = v;
}
public String toString() {
return val + "";
}
}
</details>
# 答案2
**得分**: 0
你可以使用允许使用 [正则表达式模式][1] 的方法,通过 [String#matches(String)][2] 来搜索字符串中的逻辑序列匹配。
[这里有一个页面][3] 可以帮助您创建和测试适合您需求的正则表达式。您可能还需要根据搜索的工作方式,在运行时构建灵活的模式。
但请记住,HashMap 不会保留插入键的顺序。keySet() 不会以固定顺序返回它们。如果您需要有序,可以使用 [LinkedHashMap][4]。
[1]: https://zh.wikipedia.org/wiki/正则表达式
[2]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches(java.lang.String)
[3]: https://regexr.com/
[4]: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
<details>
<summary>英文:</summary>
You could use [Regex-Patterns][1] that allow to search Strings for matchings of a logical sequence using [String#matches(String)][2].
[Here is a page][3] that might help you create and test a regex for your needs. You might also have to construct your pattern flexible during runtime, depending on how your search works.
Tho keep in mind that a HashMap does not keep the order in which the keys were inserted. keySet() does not return them in a fixed order. If you need them ordered, you could use a [LinkedHashMap][4]
[1]: https://en.wikipedia.org/wiki/Regular_expression
[2]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches(java.lang.String)
[3]: https://regexr.com/
[4]: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论