简体   繁体   中英

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 . Each Peer is something like user session container and contains UserAuthorities :

public class Peer {
  private UserAuthorities authorities;
  //...
}

I need:

Quickly do:

  1. [in half of requests] Get all peers: List<Peer>
  2. [in each request] Get all peers grouped by UserAuthorities : Map<UserAuthorities, List<Peer>> .
  3. [once for browser tab] Add new peer (with null UserAuthorities and after some time set user authorities)
  4. [arrely] Remove peer by peerUid or all peers by UserAuthorities
  5. Finally achieve all four statements above in concurrent environment

Problem:

At the moment when I add new Peer to the map, I HAVEN'T UserAuthorities . It means that UserAuthorities are muttable.

Questions:

  1. Is there any way to get Map<UserAuthorities, List<Peer>> from Map<Long, Peer> quickly? (Aproximate number of peers is about 20; but that action will be proceeded on each user request)
  2. If no, how can I achieve that if Peer.userAuthorities is mutable field?

PS: When UserAuthorities are changed?

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. 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?

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?

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? And should it be included in the map that is returned by method 2?

List<Peer> peersWithNull = map.get(null);

?

Concerning the changing UserAuthority: Is it possible to inform this data structure when the User Authority changes? That is, can the LoginDatastructure have an additional method like

void userAuthorityWasSet(Peer peer);

that will be called when the UserAuthority of the given peer changed?

Concerning point 5, the concurrency: Is there any reason not to blatantly make all of these methods 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? 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.

In any case, I have the feeling that StackOverflow might not be the right place for this kind of discussion ....

  1. Check the Map API: Map#values()
  2. ?
  3. Check the Map API: Map#remove()
  4. ?
  5. ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM