Java 如何验证HashMap中是否存在值
我有以下Java 如何验证HashMap中是否存在值,java,android,arraylist,iterator,hashmap,Java,Android,Arraylist,Iterator,Hashmap,我有以下哈希映射,其中键是一个字符串,值由数组列表表示: HashMap<String, ArrayList<String>> productsMap = AsyncUpload.getFoodMap(); 为什么不: // fast-enumerating map's values for (ArrayList<String> value: productsMap.values()) { // using ArrayList#contains
哈希映射
,其中键
是一个字符串
,值
由数组列表
表示:
HashMap<String, ArrayList<String>> productsMap = AsyncUpload.getFoodMap();
为什么不:
// fast-enumerating map's values
for (ArrayList<String> value: productsMap.values()) {
// using ArrayList#contains
System.out.println(value.contains("myString"));
}
。。。这里有一个更简单的方法来实现同样的效果,首先迭代productsMap
值,而不是foods
的内容:
// iterate productsMap values
productsMap
.values()
.stream()
// flattening to all list elements
.flatMap(List::stream)
// matching any occurrence of...
.anyMatch(
// ... an element contained in foods
(s) -> foods.contains(s)
)
您需要使用
containsKey()
方法。要做到这一点,您只需获取要从中取出密钥的hashMap,然后使用containsKey
方法,如果它返回boolean
值。这将搜索整个hashMap,而不必迭代每个项目。如果您确实拥有密钥,那么您可以简单地检索该值
它可能看起来像:
if(productsMap.values().containsKey("myKey"))
{
// do something if hashMap has key
}
这里是链接到
从Android文档:
API级别1中添加的公共布尔containsKey(对象键)
返回此映射是否包含指定的键。参数键
搜索的关键。返回
true if this map contains the specified key, false otherwise.
试试这个
Iterator<String> keySetIterator = productsMap.keySet().iterator();
Iterator<ArrayList<String>> valueSetIterator = productsMap.values().iterator();
while(keySetIterator.hasNext()){
String key = keySetIterator.next();
if(valueSetIterator.next().contains(key)){ // corrected here
System.out.println("Yes! its a " + key);
}
}
Iterator-keysiterator=productsMap.keySet().Iterator();
迭代器值SetIterator=productsMap.values().Iterator();
while(keySetIterator.hasNext()){
String key=keysiterator.next();
如果(valueSetIterator.next().contains(key)){//在此处更正
System.out.println(“是的!它是一个”+键);
}
}
如果您想使用Java 8的方法,可以这样做:
private static boolean containsValue(final Map<String, ArrayList<String>> map, final String value){
return map.values().stream().filter(list -> list.contains(value)).findFirst().orElse(null) != null;
}
private static boolean containsValue(final Map<String, ArrayList<String>> map, final String value){
return map.values().stream().filter(list -> list.contains(value)).findFirst().isPresent();
}
private静态布尔containsValue(最终映射,最终字符串值){
返回map.values().stream().filter(list->list.contains(value)).findFirst().orElse(null)!=null;
}
或者像这样:
private static boolean containsValue(final Map<String, ArrayList<String>> map, final String value){
return map.values().stream().filter(list -> list.contains(value)).findFirst().orElse(null) != null;
}
private static boolean containsValue(final Map<String, ArrayList<String>> map, final String value){
return map.values().stream().filter(list -> list.contains(value)).findFirst().isPresent();
}
private静态布尔containsValue(最终映射,最终字符串值){
返回map.values().stream().filter(list->list.contains(value)).findFirst().isPresent();
}
两者产生的结果应该几乎相同。试试以下方法:
public boolean anyKeyInMap(Map<String, ArrayList<String>> productsMap, List<String> keys) {
Set<String> keySet = new HashSet<>(keys);
for (ArrayList<String> strings : productsMap.values()) {
for (String string : strings) {
if (keySet.contains(string)) {
return true;
}
}
}
return false;
}
public boolean anyKeyInMap(映射产品映射,列表键){
Set keySet=新哈希集(键);
for(ArrayList字符串:productsMap.values()){
for(字符串:字符串){
if(键集包含(字符串)){
返回true;
}
}
}
返回false;
}
这里有一个示例方法,可以测试这两种场景。在地图的键中搜索项目,并在每个地图条目的列表中搜索项目:
private static void testMapSearch(){
final ArrayList<String> fruitsList = new ArrayList<String>();
fruitsList.addAll(Arrays.asList("Apple", "Banana", "Grapes"));
final ArrayList<String> veggiesList = new ArrayList<String>();
veggiesList.addAll(Arrays.asList("Potato", "Squash", "Beans"));
final Map<String, ArrayList<String>> productsMap = new HashMap<String, ArrayList<String>>();
productsMap.put("fruits", fruitsList);
productsMap.put("veggies", veggiesList);
final ArrayList<String> foodList = new ArrayList<String>();
foodList.addAll(Arrays.asList("Apple", "Squash", "fruits"));
// Check if items from foodList exist in the keyset of productsMap
for(String item : foodList){
if(productsMap.containsKey(item)){
System.out.println("productsMap contains a key named " + item);
} else {
System.out.println("productsMap doesn't contain a key named " + item);
}
}
// Check if items from foodList exits in productsMap's values
for (Map.Entry<String, ArrayList<String>> entry : productsMap.entrySet()){
System.out.println("\nSearching on list values for key " + entry.getKey() + "..");
for(String item : foodList){
if(entry.getValue().contains(item)){
System.out.println("productMap's list under key " + entry.getKey() + " contains item " + item);
} else {
System.out.println("productMap's list under key " + entry.getKey() + " doesn't contain item " + item);
}
}
}
}
private static void testMapSearch(){
最终ArrayList fruitsList=新ArrayList();
addAll(Arrays.asList(“苹果”、“香蕉”、“葡萄”));
最终ArrayList veggiesList=新ArrayList();
addAll(array.asList(“土豆”、“南瓜”、“豆类”));
final Map productsMap=new HashMap();
产品映射放置(“水果”,水果列表);
产品映射放置(“蔬菜”,蔬菜列表);
最终ArrayList foodList=新ArrayList();
addAll(array.asList(“苹果”、“南瓜”、“水果”));
//检查productsMap的键集中是否存在foodList中的项
for(字符串项:foodList){
if(产品映射容器(项目)){
System.out.println(“productsMap包含一个名为“+项”的键);
}否则{
System.out.println(“productsMap不包含名为“+项”的键);
}
}
//检查来自foodList的项目是否存在于productsMap的值中
对于(Map.Entry:productsMap.entrySet()){
System.out.println(“\n搜索键“+entry.getKey()+”的列表值”);
for(字符串项:foodList){
if(entry.getValue()包含(项)){
System.out.println(“键“+entry.getKey()+”下的productMap列表包含项“+item”);
}否则{
System.out.println(“键“+entry.getKey()+”下的productMap列表不包含项“+item”);
}
}
}
}
结果如下:
productsMap不包含名为Apple的密钥
productsMap不包含名为Squash的键
productsMap包含一个名为fruits的键
正在搜索关键水果的列表值
主要水果下的productMap列表包含Apple项
关键水果下的productMap列表不包含项目挤压
关键水果下的productMap列表不包含项目水果
正在搜索关键蔬菜的列表值
关键蔬菜下的productMap列表不包含项目Apple
主要蔬菜下的productMap列表包含南瓜
关键蔬菜下的productMap列表不包含项目水果
将检查HashMap是否包含值您的
键
值是否应该是特定字符串?是。它应该是一个特定的字符串。“未成功”不是问题描述。没有循环的替代方法productsMap.toString()包含(键)。当然,可能会有一些问题,比如,键可以是一些不同值的一部分,或者映射键,这不适用于大型映射。无论如何,这只是一个选项。如果“myString”值不完全已知,我还有一些疑问,我只知道myStri。如何验证这一点或使用包含一些字符作为“myStri”来查找完整值。你能帮我个忙吗。@Mena.或者其他人为什么它叫containsKey而不是containsValue?如果目的是要知道它是否包含值。为什么不使用productsMap.containsValue(“myKey”)
@aldok,那么在查找值时没有任何问题。@EJP
private static void testMapSearch(){
final ArrayList<String> fruitsList = new ArrayList<String>();
fruitsList.addAll(Arrays.asList("Apple", "Banana", "Grapes"));
final ArrayList<String> veggiesList = new ArrayList<String>();
veggiesList.addAll(Arrays.asList("Potato", "Squash", "Beans"));
final Map<String, ArrayList<String>> productsMap = new HashMap<String, ArrayList<String>>();
productsMap.put("fruits", fruitsList);
productsMap.put("veggies", veggiesList);
final ArrayList<String> foodList = new ArrayList<String>();
foodList.addAll(Arrays.asList("Apple", "Squash", "fruits"));
// Check if items from foodList exist in the keyset of productsMap
for(String item : foodList){
if(productsMap.containsKey(item)){
System.out.println("productsMap contains a key named " + item);
} else {
System.out.println("productsMap doesn't contain a key named " + item);
}
}
// Check if items from foodList exits in productsMap's values
for (Map.Entry<String, ArrayList<String>> entry : productsMap.entrySet()){
System.out.println("\nSearching on list values for key " + entry.getKey() + "..");
for(String item : foodList){
if(entry.getValue().contains(item)){
System.out.println("productMap's list under key " + entry.getKey() + " contains item " + item);
} else {
System.out.println("productMap's list under key " + entry.getKey() + " doesn't contain item " + item);
}
}
}
}
hashMap.containsValue("your value");