+ 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/)
 

 
 
 
 
Sk Sk 2Sk Sk 2
Attitude Status (https://attitude-status.com)
lakhwinder singh 9lakhwinder singh 9
Indian Tollywood industry lots of song and movie release daily we have latest collection of 2019 and 2020 movie song For your facebook Whatsapp Instagram Status best Collection whatsapp status tamil video download  (https://whatsappsongstatus.com/tamil-whatsapp-status-video)You Can first view video on website if you like it then you can download it and share on your status . you can also update Love Status For Your Love One and impress.
Punjabi Song All Time Hit And favourite for everyone. we cut 30 second status videos from daily release new songs. you can update Punjabi Status (https://whatsappsongstatus.com/punjabi-status-videos)  for you love one and husband wife we have latest collection of status song from singer diljit singh honey singh sidhu moosewala karan aujla jassi gill babbu mannn and other punjabi singer
Suresh hansaSuresh hansa
 Top 1 Website For Quotes for reading (https://www.hindiattitudestatus.in)
Attitude Status: (https://www.hindiattitudestatus.in/2020/03/attitude-status-in-hindi-for-whatsapp.html)
Website: https://www.hindiattitudestatus.in
Top 2 Website for Sarkari Bank Jobs (https://www.sarkaribankjobs.com)
Ipbs po, Sbi Jobs,  IBPS PO, IBPS Clerk, SBI Clerk visit: https://www.sarkaribankjobs.com