Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap with String and Arraylist - Error Message

I am getting an error message.

Suspicious call to java.util.Map.ContainsValue Given object cannot contain instance of String (Except ArrayList)

This is a small version of the program that I am working with. Could someone suggest how to fix this? Please post code. I am not a strong programer.

import java.util.ArrayList;
import java.util.HashMap;


public class Main {
  public static void main(String[] a) {
    HashMap<String,ArrayList> map = new HashMap<String,ArrayList>();
    //hashMap.put(key, new ArrayList());
    map.put("key1", new ArrayList());
    map.get("key1").add("value2");

    //System.out.println(map.containsKey("key1"));
    System.out.println(map.containsValue("value2"));
  }
}
like image 503
user716255 Avatar asked Feb 03 '26 13:02

user716255


1 Answers

You have an HashMap that has Strings for keys and ArrayLists for values:

HashMap<String,ArrayList> map = new HashMap<String,ArrayList>();

You then try and see if it contains a value which is a String

System.out.println(map.containsValue("value2"));

That's very suspicious. Because it can't ever have that.

like image 150
Brian Roach Avatar answered Feb 06 '26 02:02

Brian Roach