+ Start a Discussion
Heiko KramerHeiko Kramer 

How to NOT add a Key with empty Value to a Map?

// Map to keep key (caregory) and value related to each other
        Map<String, String> mailValues = new Map<String, String>();
        
        // Seperate keys and values and put it in the map 
       	List<String> listOfLines = plainText.split('\n');
        for(String line : listOfLines){
            String key   = line.substringBetween('<', '>');
            String value = line.substringBetween('<' + key + '>', '</' + key + '>');
            
            if (key != null) {                                    // this works just fine, null key not added 

            // if (key != null || value != null){           // works partial, null key not added, but empty value added
            //  if (key != null || value != ''){              // doesn't work at all, null key + empty value get added 

                mailValues.put(key, value);                
            }
Hi guys, I'm getting tagged values per mail in this format:

<key>value</key>
<key2>value</key2>

Somethimes there are linebreaks, so I get a null key added to the map. Got that solved with "key != null".

But I'm struggling with excluding no-values like <key></key>.
Probably easy, but I'm absulte beginner … can someone give me a hand here?
 
Best Answer chosen by Heiko Kramer
Sourav PSourav P
Hi,
Why not to use only , If (value != null) { ,in the second If statement. Using both key & value in the If statement  will generate the key, value pair if any one is true.

All Answers

Sourav PSourav P
Hi,
Why not to use only , If (value != null) { ,in the second If statement. Using both key & value in the If statement  will generate the key, value pair if any one is true.
This was selected as the best answer
Heiko KramerHeiko Kramer
Oh … because it is an OR condition, of course. Thank's a lot Sourav :)
Heiko KramerHeiko Kramer
If (value != null) {  // doesn't actually work

// but this works when it stays alone: 
If (value != '') {


Although there is no space between the tags, the value is apparently not null.
Heiko KramerHeiko Kramer
if (value != ''){
// doesn't do the full job … when there is a line break, key and value are null, so key get's added.

if (key != null && value != ''){
// that's my final solution, works for both conditions.

 
nikhil nikhil 5nikhil nikhil 5
Hi,
Why not to use only , If (value != null) { ,in the second If statement. Using both key & value in the If statement  will generate the key, value pair if any one is true.    Hack Instagram
Whereami WhereamiWhereami Whereami
A map can orient you thru displaying you your environment. Every day, whether or no longer visiting a contemporary day city or wandering thru well-known streets, humans pull out smartphones and use its place to discover extra. In this put up, we’ll display you a way to discover what your place is proper now, Where am I on both mobile phones and traditional internet browsers. Even extra beneficial, we’ll display you the manner to help others make some experience of a place you comprehend properly. With your information and your buddy’s location, you’ll be capable of create a palms-off tour experience.
 
shubham sharma 200shubham sharma 200

hi Heiko,

this coding is really sick because this is really old version i also creat a website with wordpress you can cheack it now thank you 
AutoMartz (https://www.automartz.com)
attitude for girls (https://goodmorninglove.in/50-positive-attitude-quotes-in-hindi-and-english-for-girl
good night  (http://https://goodmorninglove.in/good-night-quotes/)

hindi love quotes (https://goodmorninglove.in/hindi-love-quotes/)
love shayari in hindi (https://goodmorninglove.in/love-shayari-hindi/
good morning quotes (https://goodmorninglove.in/good-morning-quotes/)