How do i store multible objects in a property of another class?

Lets assume that I have two classes “User” and “UserManager”. Any instance of “User” should be added to a property “ListOfUsers” of “UserManager”. “Usermanager” should be able to delete/add new Users and get the instance of a specific user (via user id).

Could someone give me some kind of example coding for something like this?

1 Like

No one? :>
I believe i need to use mutableList or mutableMap, but I don’t know how. Can anybody help me? I can’t find an example for object types, only for integer, string etc.

The simplest version I guess would be something like this:

class UserManager {
	private val users = ConcurrentHashMap<UserId, User>()
	
	fun findById(id: UserId): User? = users[id]
	
	fun findAll(): Collection<User> = users.values // either collection copy or just a read-only projection
	
	// other CRUD methods and the rest would go with a similar approach
}
1 Like