It sounds like you are looking for a data structure to store a list of messages per user. You can use a Map<User, List<Message>
for this. When loading/adding a User
, you can create an empty ArrayList<Message>
and put it into the map for later use.
// Create map.
Map<User, List<Message>> userMessageMap = new HashMap<>();
// Insert new list for new user.
userMessageMap.put(user, new ArrayList<>());
// Insert message for existing user.
userMessageMap.get(user).add(message);
// Get all messages for an existing user.
List<Message> messages = userMessageMap.get(user);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…