0

I don't know why does not work with me. I have a empty keys in map but I cannot catch them.

Map<String, Long> map = new TreeMap<>();

//put data

map.entrySet().stream().forEach(e -> {
    if (e.getKey().equals("") || e.getKey().equals(" ") || e.getKey() == "" || e.getKey() == " ") {
        map.remove(e.getKey(), e.getValue());
    }
});

Edited: I made a test for value:

     map.entrySet().stream() .forEach(e -> {
            if (e.getValue() == 133835) {
            System.out.println("key empty: " + e.getKey().isEmpty());
            System.out.println("key: >" + e.getKey() + "<");
            System.out.println("val: " + e.getValue());
        }
    });

    map = map.entrySet().stream().filter(
            p -> !"".equals(p.getKey().trim())).
            collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


map.entrySet().stream() .forEach(e -> {
    if (e.getValue() == 133835) {

        System.out.println("key empty: " + e.getKey().isEmpty());
        System.out.println("key: >" + e.getKey() + "<");
        System.out.println("val: " + e.getValue());
    }
});

the result is:

key empty: false
key: >‏<
val: 133835
key empty: false
key: >‏<
val: 133835

I think this key is a gost )

7
  • "I have a empty keys in map but I can catch them." - this sentence makes no sense. What are you trying to do, and what's happening instead of what you were trying to make happen? Commented Dec 11, 2014 at 21:54
  • can you specify what the issue is? what does not work for you? what are you expecting to happen? how do you put the data? Commented Dec 11, 2014 at 21:54
  • From the edit, it seems he can'T catch/find the keys that are empty strings in his map. Commented Dec 11, 2014 at 21:56
  • Sorry for this mistake, I changed it to "cannot" Commented Dec 11, 2014 at 22:13
  • Obviously it's not empty. Figure out what's in it-where are the keys coming from. Commented Dec 11, 2014 at 23:10

2 Answers 2

1

Try using e.getKey().trim().isEmpty()

Sign up to request clarification or add additional context in comments.

3 Comments

I have now just System.out.println("found") this is not the problem
key.trim().isEmpty() sounds like the right way to check this. There may be exotic characters that are non-printing in your locale which trim() does not remove.
It still the same result: key empty: false
0

first of all i would generally suggest to trim instead of checking for " " m(one space) and "" (empty string). And secondly that might be the case (a key with more than one space).

  "".equals(e.getKey().trim())

edit example: Map map = new TreeMap();

        map.put("",  1L);
        map.put(" ", 2L);
        map.put("3", 3L);

        map.entrySet().stream().forEach(e -> {
            System.out.println("key empty: " + e.getKey().isEmpty());
            System.out.println("key: >" + e.getKey() + "<");
            System.out.println("val: " + e.getValue());
        });
        map = map.entrySet().stream().filter(
                p -> !"".equals(p.getKey().trim())).
                collect(Collectors.toMap(Entry::getKey, Entry::getValue));        

        map.entrySet().stream().forEach(e -> {
            System.out.println("key empty: " + e.getKey().isEmpty());
            System.out.println("key: >" + e.getKey() + "<");
            System.out.println("val: " + e.getValue());
        });

output is:

key empty: true
key: ><
val: 1
key empty: false
key: > <
val: 2
key empty: false
key: >3<
val: 3
AFTER REMOVAL
key empty: false
key: >3<
val: 3

12 Comments

How does this answer the question?
deosn't answer it but might be a lead in the right direction. for me it appears an empty key for him is defined as an String matching one blank or "". So maybe a String with two bnalks qulifies as empty as well and will not be fdund by his code.
tried a filter?, something like: m.entrySet().stream() .filter(p -> !"".equals(p.getKey().trim())) .collect(toMap(Entry::getKey, Entry::getValue));
How can I make this mothod toMap() ?
sry: Collectors.toMap (disclaimer: have not java8 compiler at hand).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.