-2

In Java, I am implementing this:

Map<String, List<String >> mdaps = new HashMap<String, List<String >>();

I want to display the list that contains the word "Scored" like this:

Scored [Profile]
Scored [Applicability]

But how to search in entry.getkey()?

as it does not work with: if(entry.getKey().contrains("Scored"))

Here is my code: Edit1

Map<String, List<String >> mdaps = new HashMap<String, List<String >>();

          List<String > List1 = new ArrayList<String>();
          List<String > List2 = new ArrayList<String>();
          List<String > List3 = new ArrayList<String>();

          List1.add("Profile");
          List2.add("Applicability");
          List3.add("Level 1");

      mdaps.put("(Scored)", List1 );
      mdaps.put("(Scored)", List2 );
      mdaps.put("Not Scored", List3 );

      for(Map.Entry<String, List<String>> entry : mdaps.entrySet()){

              if(entry.getKey().contains("(Scored)")) // not Working
          System.out.println(entry.getKey()+" "+ entry.getValue());
      }
    }

DemoProgramm

thanks in advance.

Michael1
  • 253
  • 1
  • 6
  • 19
  • 7
    `contrains` -> `contains` ?? – OldCurmudgeon May 25 '17 at 13:18
  • 2
    try entry.getKey().equals("Scored"). you have add two item with same key. but it will replace first item – Md Ayub Ali Sarker May 25 '17 at 13:21
  • 1
    1. The code contains another bug: you are overriding `List1` with `List2` when you insert it using the same key into `mdaps`. 2. Use Java conventions when you're naming variable: List1 --> list1 and etc. 3.Try to give a more meaningful names to your variables. – Nir Alfasi May 25 '17 at 13:22
  • 1
    Have you even looked at the page that you linked yourself? ("DemoProgramm" link) It clearly shows at the bottom: `Main.java:28: error: cannot find symbol if(entry.getKey().contrains("(Scored)"))` – Erwin Bolwidt May 25 '17 at 13:22
  • 2
    A `HashMap` can hold only unique keys - so when you `put` `List2` after `put`ting `List1`, `List1` gets replaced by `List2` in the map. Also, the method is called `contains`, not `contrains` (note the absence of the 'r'). You'd want what we call a `MultiMap`, which allows for multiple values per key - and you can find details of one at https://stackoverflow.com/questions/18922165/how-to-include-duplicate-keys-in-hashmap – Tamoghna Chowdhury May 25 '17 at 13:29
  • Thank you to you, the problem is solved :) – Michael1 May 25 '17 at 13:31

3 Answers3

1

entry.getKey() will return a set and that will contain your key so it will always return true.

for (Map.Entry<String, List<String>> entry : mdaps.entrySet()) {

            if (entry.getKey().equals("Scored")) // not Working
                System.out.println(entry.getKey() + " " + entry.getValue());
        }
gati sahu
  • 2,576
  • 2
  • 10
  • 16
0

The solution is to replace contrains by contains:

for (Map.Entry<String, List<String>> entry : mdaps.entrySet()) {

            if (entry.getKey().contains("(Scored)")) 
                System.out.println(entry.getKey() + " " + entry.getValue());
        }
Michael1
  • 253
  • 1
  • 6
  • 19
0

You Are Over writing the value of key "(Scored)" by putting again using the same key . So List1 is not displaying in sysout. You should not use same key for different values. HashMap uses hasCode() of key to store the values and String having same value will have same hascode. and it thinks that its the same. hence over writes the value.

jkamani
  • 51
  • 3