-2

I have a list of Notification's objects, its name is notifications.I map it in a HashMap:

Map<String, String> recipientByMedia = new HashMap<>();
        for (Notification obj : notifications) {
            recipientByMedia.put(obj.getMedia(), obj.getRecipient());
        }

my notification list have tow different value for "IM" :

"IM" : "+928292929"
"IM" : "test@yahoo.com"

I know, HashMap overrides new "IM" instead oldest one. finally, I want to use this HashMap to using in "Mockoto.assertEqual" .like this:

assertEquals(recipientByMedia.get("IM"), contact.getSmsNumber());
assertEquals(recipientByMedia.get("IM"), contact.getImAddress());

one of these test will fail. how can I compare "IM" in my test? which data structure can I use instead of HashMap?

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
m.mjn202
  • 43
  • 1
  • 9

3 Answers3

1

i think, it is a feature of hash map which makes a one to one relation between domain and range.

I suggest you to make different keys. like

assertEquals(recipientByMedia.get("IM0"), contact.getSmsNumber());
assertEquals(recipientByMedia.get("IM1"), contact.getImAddress());
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
1

Since the class Notification seems to be the single source of truth, why do you need a different data structure? What are you trying to accomplish with the Map?

If you want to look up the properties of the Notification object in your list where getMedia is let's say "IM" you could just filter the list and access the properties directly from the object that was meant to hold this kind of information:

String recipient = notifications.stream()
  .filter(n -> "IM".equals(n.getMedia()))
  .findFirst()
  .orElseThrow(() -> new IllegalArgumentException(/* not in the list */))
  .getRecipient();

If this is not the case and you want to compose data from multiple sources, create POJOs for the exact purpose of holding the necessary information, or simply use the 2 data sources to get the combined information if it's more readable for some purpose.

Community
  • 1
  • 1
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
0

Create a Model with setter and getter and then use ArrayList instead of Hashmap like this private ArrayList<StudentsDetail> arraylist; and then inside of your for loop add this arraylist.add(new StudensDetail(obj.getMedia(), obj.getRecipient())); and when you wanted to get values write this code inside your for loop id = arraylist.get(i).getId();

and this is the model

public class StudentDetails {
private String name;
private String id;

public StudentDetails(String name, String id) {
    this.name = name;
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

Matiullah Karimi
  • 1,234
  • 1
  • 15
  • 17
  • thank you. it is great. but, How can I compare these valued???? "IM" has tow values and I want to test both values. How can I do it???? – m.mjn202 Jan 30 '17 at 10:07