- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个程序来验证 map 是否包含某些值集。 Map
API 有一种称为 map.containsValue(string)
的方法。但是,此方法将完整的字符串验证为值。
import java.util.HashMap;
import java.util.Map;
public class TestMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(6, "P_T");
map.put(7, "P_Q_T");
map.put(8, "T");
map.put(9, "A");
map.put(10, "B");
map.put(11, "P_A");
map.put(1, "P_1");
map.put(2, "Q");
map.put(3, "P_Q");
map.put(4, "Q_T");
map.put(5, "T");
System.out.println("Map is = "+map);
System.out.println("Result is = " + map.containsValue("P"));
}
}
以上程序将返回输出为:
Map is = {1=P_1, 2=Q, 3=P_Q, 4=Q_T, 5=T, 6=P_T, 7=P_Q_T, 8=T, 9=A, 10=B, 11=P_A}
Result is = false
我的要求是 Result
应该为真,因为 Map 包含键 1、3、6、7、11,其中包含 char P
作为值。
我有一个解决方案,我可以使用循环来验证每个值,然后为 P
字符找到 indexOf
每个值。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(6, "P_T");
map.put(7, "P_Q_T");
map.put(8, "T");
map.put(9, "A");
map.put(10, "B");
map.put(11, "P_A");
map.put(1, "P_1");
map.put(2, "Q");
map.put(3, "P_Q");
map.put(4, "Q_T");
map.put(5, "T");
System.out.println("Map is = " + map);
System.out.println("Result is = " + map.containsValue("P"));
System.out.println("Result from loop is = " + verifyMap(map));
}
private static boolean verifyMap(Map<Integer, String> map) {
// TODO Auto-generated method stub
Set<Integer> set = map.keySet();
Iterator<Integer> itr = set.iterator();
while (itr.hasNext()) {
Integer integer = (Integer) itr.next();
String str = map.get(integer);
if (str.indexOf("P") >= 0)
return true;
}
return false;
}
}
我用 indexOf
方法而不是 contains
评估了 string
,后者稍微快一些。
这将返回所需的结果:
Result from loop is = true
但是,我只想知道,有没有其他方法可以验证?
最佳答案
您可以使用 Java 8 Streams 用更少的代码编写您的方法:
private static boolean verifyMap(Map<Integer, String> map) {
return map.values().stream().anyMatch(str->str.indexOf("P") >= 0);
}
即使在 Java 7 中,您的方法也可以更短:
private static boolean verifyMap(Map<Integer, String> map) {
for (String str : map.values()) {
if (str.indexOf("P") >= 0)
return true;
}
return false;
}
关于java - 如何用部分字符串验证 HashMap containsValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33820524/
我正在尝试使用 HashMap 和数组为 6 支球队创建一个赛程列表。我有 6 个团队。每支球队必须互相比赛两次。我将从原始团队列表中随机删除每个团队并将其添加到新列表中。这个新列表被添加到 Hash
我有一个巨大的 float 组,我必须从中删除重复项。我尝试创建一个哈希表来填充唯一值并将其传递回另一个数组并返回它。问题出在 containsValue 方法中,该方法总是返回 false,因此所有
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 4 年前。 我正在尝试使用字典单词的哈希表编写拼写检查器。我目前正在尝试将文本文档
我有一个 HashSet,其中包含通过读取二进制文件生成的自定义对象。我还有一个通过读取 DBF 文件的每一行生成的字典。两者都有一个索引属性,它们相互对齐。例如,我的词典中的第 10 项将与我的 H
我正在开发一个程序来验证 map 是否包含某些值集。 Map API 有一种称为 map.containsValue(string) 的方法。但是,此方法将完整的字符串验证为值。 import jav
我有一个 HashMap,我需要通过它的整数值来获取一个项目。我注意到有一个 containsValue() 函数,但看起来我仍然必须遍历 map 才能找到正确的索引。 我的问题是;如果之后需要遍历它
我有一个字典,我在其中将一个键和 2 个值存储在一个元组中,如下所示: variableDictionary = new Dictionary>(); 所以我的字典是:key, 我想检查 value
我得到了一个O(n)时间复杂度的问题: “给定一个数字列表和数字 x。查找列表中是否有 2 个数字加起来为 x?” 这是我的解决方案: public class SumMatchResult {
因此,我试图在 powershell 中创建一个函数,该函数将用户所属的所有组嵌套在广告中。我将这些组映射到一个哈希表中,使用 AD 中的通用名称作为值。 这很顺利,该函数返回一个包含我所有组的哈希表
为什么会发生这种情况以及如何避免?我认为它只检查引用,但这不是很方便。 import java.util.LinkedHashMap; public class CheckingLinkedHashM
java中的containsValue是如何工作的?它是否将给定对象与 map 中已有的值进行比较?如果是这样,为什么这不起作用? 我有以下类:class Node{ 整数n; 整数重量; 字符串 v
如果我在类型和值中输入了错误的值,我希望我的程序显示错误消息。 错误消息适用于类型,但不适用于值。这是怎么回事? Map Heart = new HashMap(); Heart.put("A", "
我正在尝试将我用 Java 编写的算法转换为 Scala,但我在使用 Java 中的 containsValue() 方法时遇到了问题。我想做一些类似于 if (hashMap.containsVal
我有一本映射到摩尔斯电码的所有字母表 Dictionary data = new Dictionary(); data.Add("a", ".-"); data.Add
我有一张 map 如下: final Map> existingNames = this.getExistingCIs(_actual,_names.keySet()); 它返回: {myobj43c
containsValue() 在 AbstractMap 类中实现如下: public boolean containsValue(Object value) { Iterator> i =
我有一个下面的程序,其中有一个 HashMap 。 HashMap 的键是简单的整数,值是整数数组。程序如下: Map myMap = new HashMap(); myMap.put("EvenN
我正在实现一个 HashMap,即使字符重复,它也总是返回 false。我尝试了其他堆栈溢出问题中给出的解决方案,但没有帮助,带有函数 contains() 的哈希表也是如此。 HashMap ht
我想打印从扫描仪对象到达这里的选定键和值。我尝试过这种方式,发现有区别: TreeMap oldbooks = new TreeMap(); oldbooks.put("WaltDis
是否有有效的方法来检查字典中的值,一旦知道该值存在,然后找到该值的键? //检查字典已经有sortedWord(即key是word,value是sortedWord) if (anagramDict
我是一名优秀的程序员,十分优秀!