简体   繁体   中英

How to retrieve a collection of values from a HashMap

I'm writing a function to test if a HashMap has null values. The method .values() SHOULD return a collection of just the values, but instead I receive a map with both the keys and values stored inside. This is no good as the purpose of my function is to check if the values are null, but if I return a map with keys AND values then .values().isEmpty() returns false if I have a key stored with no value.

  public Map<KEY, List<VALUES>> methodName() {
    if (MAPNAME.values().isEmpty()) {
            throw new CustomErrorException(ExceptionHandler.getErrorWithDescription(ErrorConstants.ERROR_MSG_01));
        } else {
            return MAPNAME;
        }
    }

In the above example, .values() always returns a map containing all the keys and values. My method never throws a CustomErrorException if the HashMap has a key, which is bad since it's supposed to detect if there are no values. Help!

There's no such thing as a Map implementation that has a key stored without a value. All Map implementations either:

  • throw an exception in put when the value is null
  • Add an entry with a key and a value of null

A key that maps to null is very different than a key without a value. The key has a value, and that value is null (and that means that the values collection won't be empty, unless the map is empty). A key without a value is a key that's not contained in the map.

Long story short, you probably want to use MAPNAME.values().contains(null) or even just MAPNAME.containsValue(null) to do what you want. Alternatively, if you're checking that every key maps to null, check that by iterating over the .values() collection.

您正在返回地图 - MAPNAME,而不是值:

return MAPNAME.values();

If you're trying to determine if the map contains any null values, you should iterate over the collection of values and check each one to see if its null.

A map that contains an entry with a null value is not empty.

You're not being very clear about what you want -- your map values are lists -- considering that, there are three ways to have a key map to "no values":

  • A key mapped to null (then the test is map.values().contains(null) )
  • A key mapped to an empty list (then the test is map.values().contains(Collections.emptyList()) )
  • A key mapped to a list full of null s.

What your method above is doing right now is throwing an exception if the map is truly empty (no keys), and returning the map otherwise.

It is not clear what you want. If you want the method to throw an exception only if the map has no meaningful values (all keys map either to null or to empty lists) then something like this is what you need:

public Map<KEY, List<VALUES>> methodName() {
    for( List<VALUES> values : MAPNAME.values() ) // 1
        if( null != values ) // 2
            for( VALUES value : values ) // 3
                if( null != value ) // 4
                    return MAPNAME;

    throw new CustomErrorException(ExceptionHandler.getErrorWithDescription(ErrorConstants.ERROR_MSG_01));
}

This throws an exception in the all reasonably conceivable "empty map" scenarios -- if (1) the map is truly empty, or (2) it contains only null keys, or (3) it only contains only null values or empty lists, or (4) it contains only null values or empty lists or lists of null s.

(Levels of "emptiness" tests in the text above correspond to the comment labels in the code).

Use the values() method in this way:- Collection set=MAPNAME.values();

And then use a foreach loop to check if every value is null or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM