0

I want to do something like this:

1 -> imran
2 -> ajmal
3 -> habib
4 -> saurab
4 -> tendulkar

If I use get(1) I get

imran

Now if I use get(4) I should get

saurab
tendulkar

Is this possible to do?

avocadoLambda
  • 1,332
  • 7
  • 16
  • 33

1 Answers1

0

So fir any one key, you want to return more than one or more objects in the result. One of more objects implies a List.

So you could have :

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

void add(Integer key, String value) {

    List<String> list = myMap.get(key);

    If (list==null) {
        list = new ArrayList<>();
        myMap.put(key, list);
    }
    list.add(value);
}
racraman
  • 4,988
  • 1
  • 16
  • 16