[英]Rapid way to represent Map<K, V> as Map <U, List<V>>
I have: Map<Long, Peer>
, where key is peerUid
and value
is Peer
. 我有: Map<Long, Peer>
,其中键是peerUid
, value
是Peer
。 Each Peer
is something like user session container and contains UserAuthorities
: 每个Peer
都类似于用户会话容器,并且包含UserAuthorities
:
public class Peer {
private UserAuthorities authorities;
//...
}
I need: 我需要:
Quickly do: 快速执行:
List<Peer>
[在一半请求中]获取所有同级: List<Peer>
UserAuthorities
: Map<UserAuthorities, List<Peer>>
. [在每个请求中]获取按UserAuthorities
分组的所有对UserAuthorities
: Map<UserAuthorities, List<Peer>>
。 peerUid
or all peers by UserAuthorities
[很少]通过peerUid
删除对等peerUid
或通过UserAuthorities
删除所有对等方 Problem: 问题:
At the moment when I add new Peer
to the map, I HAVEN'T UserAuthorities
. 当我在地图上添加新的Peer
,我没有UserAuthorities
。 It means that UserAuthorities
are muttable. 这意味着UserAuthorities
是可变的。
Questions: 问题:
Map<UserAuthorities, List<Peer>>
from Map<Long, Peer>
quickly? 有什么方法可以快速从Map<Long, Peer>
获取Map<UserAuthorities, List<Peer>>
吗? (Aproximate number of peers is about 20; but that action will be proceeded on each user request) (对等体的数量大约为20;但是将根据每个用户请求执行该操作) PS: When UserAuthorities
are changed? PS:更改UserAuthorities
?
The answer is very simple - only once on log in. User goes to home page, he get Peer
on the server side, but haven't got any authorities yet. 答案非常简单-只需登录一次。用户进入主页,他在服务器端获得了Peer
,但还没有任何权限。 So he loges in and get them. 因此,他登录并获取了它们。
This is not an answer, so technically, it might have to be deleted - But something like this can hardly be discussed in a comment...: 这不是答案,因此从技术上讲,它可能必须删除-但是类似这样的内容很难在评论中讨论...:
So I assume that the data structure should look like this interface LoginDatastructure
, is this correct? 因此,我认为数据结构应类似于此接口LoginDatastructure
,这正确吗?
import java.util.List;
import java.util.Map;
class UserAuthorities {}
class Peer
{
private UserAuthorities authorities;
}
interface LoginDatastructure
{
// 1. Get all peers
List<Peer> getAllPeers();
// 2. Get all peers grouped by UserAuthorities
Map<UserAuthorities, List<Peer>> getPeersByUserAuthority();
// 3. Add new peer (with null UserAuthorities
// and after some time set user authorities)
void addPeer(Peer peer);
// 4a Remove peer by peerUid
void removePeer(long peerUid);
// 4b. Remove peer by peerUid or all peers by UserAuthorities
void removePeers(UserAuthorities u);
}
Where does your Map<Long, Peer>
occur in this example? 在此示例中Map<Long, Peer>
您的Map<Long, Peer>
出现在哪里?
Concerning 3.: When the peer is added, and it has a null
UserAuthority, should it be included in the list that is returned by method 1? 关于3 .:添加对等方且其UserAuthority为null
,是否应将其包含在方法1返回的列表中? And should it be included in the map that is returned by method 2? 并且应该将其包含在方法2返回的地图中吗?
List<Peer> peersWithNull = map.get(null);
? ?
Concerning the changing UserAuthority: Is it possible to inform this data structure when the User Authority changes? 关于更改的UserAuthority:是否可以在用户权限更改时通知此数据结构? That is, can the LoginDatastructure
have an additional method like 也就是说, LoginDatastructure
是否可以具有其他方法,例如
void userAuthorityWasSet(Peer peer);
that will be called when the UserAuthority of the given peer changed? 给定对等方的UserAuthority更改时将调用什么?
Concerning point 5, the concurrency: Is there any reason not to blatantly make all of these methods synchronized
? 关于第5点,并发性:是否有任何理由不公然地synchronized
所有这些方法?
EDIT: Concerning the synchronization: What kind of thread-safety do you expect? 编辑:关于同步:您期望什么样的线程安全? For example, when one Peer is added, should this automatically become visible in all Lists that someone may have obtained from the Map of method 2? 例如,当添加一个Peer时,它是否应该自动出现在某人可能从方法2的映射中获得的所有列表中? Should these lists also be updated when the authority is set? 设置权限后是否还应更新这些列表? In any case, until now, the methods are primarily adding/removing elements to/from the data structure, so I think making them synchonrized
would not have any considerable performance impact (unless, of course, the data structures, like the map, are rebuilt from scratch during each call - but I think you wanted to avoid this anyhow. 无论如何,到目前为止,这些方法主要是在数据结构中添加元素/从数据结构中删除元素,因此我认为使其synchonrized
不会对性能产生任何重大影响(当然,除非数据结构(如地图)在每次通话中从头开始重建-但是我认为您还是想避免这种情况。
In any case, I have the feeling that StackOverflow might not be the right place for this kind of discussion .... 无论如何,我觉得StackOverflow可能不是进行此类讨论的正确位置。
Map
API: Map#values()
检查Map
API: Map#values()
Map
API: Map#remove()
检查Map
API: Map#remove()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.