-2

I am working on a personal project on Java. I have a Map called allow and the second parameter is another Map. I am trying to compare the second parameter of the Map inside allow. If anyone can help me that would be a big help.

public boolean checkBank(String bank, int cNumber){
    Map <String, Map<String, String> > allow = new HashMap<>();
    String num = Integer.toString(cNumber);
    Iterator<Map.Entry<String, Map<String, String>>> entries = allow.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, Map<String, String>> entry = entries.next();
        if (entry.getKey().equals(bank)) {
            String all = entry .getValue().get(0);
            for (int i = 0; i < entry.getValue().size(); i++) {
                if(entry.getValue().equals(num)) {
                    return true;
                }
            }
        }
    }
    return false;
}
  • You question needs more information (like what is 'allow' here?) in order to provide the solution. – Ram Mar 04 '18 at 04:53
  • It's very unclear what you're trying to do and what's going wrong. Please elaborate. – shmosel Mar 04 '18 at 04:55
  • 1
    Possible duplicate of [Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop](https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – Fenix Mar 04 '18 at 06:04
  • `allow`(`allow = new HashMap<>();`) is a blank map. It has no content. your `entries` iterator has no value to iterate. – triandicAnt Mar 04 '18 at 07:05

1 Answers1

-1

On the statement: if(entry.getValue().equals(num)) entry.getValue() is a Map, but num is a string. These two are not compatible types, so they can never be equal.

It's worth noting that you are looking for the one entry with the key value equal to bank. Rather than scan through all Map.Entry objects for the one which has the right value, why not just use the statement:

Map<String,String> map = allow.get(bank);

Let the outer map do this work for you.

Your question didn't exactly make clear what you wanted, but I'm guessing that you either want to look, in the inner Map, for an Entry where either the key or the value matches num. You can do that with either

map.containsKey(num)

or

map.containsValue(num)

Is that basically what you are looking for?

  • If you have question(s) in your answer, ask it in the comment section (below the question) rather than posting it as a solution. Once you are clear with the question then post a solution. – Ram Mar 04 '18 at 05:47