I’m trying to sync to user list by building a map that contains a username as a key and an user object as the value. Once the map has been built, I’m having difficulties retrieving the object values. Below is the code I’m using.
private Map<String, User> map = new HashMap<String, User>();
I’m populating the map key and object as followed.
List<User> users = session.createCriteria(User.class).list();
for(ApplicationUser user : users) {
map.put(user.getUserName(), user);
}
I’m trying to retrieve the data as followed,
List<Person> persons = session.createCriteria(Person.class).list();
for (Person p : persons) {
User user = map.containsKey(p.getUsername()) ? map.get(p.getUsername()) : new User();
//Email is always null.
System.out.println(user.getEmail());
}
Could someone help point me in the right direction on how to retrieve the object values from my map. Thanks
First, you should change your loop to this:
Then set a breakpoint in the loop and debug your code. It looks correct so far. Maybe you are just not setting
emailinUseror the users frompersonsare not in yourmap.