-1

I have a Post class which contains 3 properties: userId, userName, postId. Every post generates a unique id(String) in the database, so I save it in a HashMap, where the Key is the unique post id, and the value is the Post, like so:

HashMap<String, Post> posts = new HashMap<>(); posts.put(postId, new Post(userId, userName, postId)).

And now I want to find all the posts in the map, with a specific userId. How to do that?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
pileup
  • 1
  • 2
  • 18
  • 45
  • 3
    No special magic for that. Just iterate over the values and select matching ones. – yegodm Dec 22 '18 at 19:13
  • Related: [Java Hashmap: How to get key from value?](https://stackoverflow.com/questions/1383797/java-hashmap-how-to-get-key-from-value) There are more similar questions, just search. – Ole V.V. Dec 22 '18 at 19:38
  • Season’s greetings and welcome to Stack Overflow. Please tell us what your search and research brought up and how it was insufficient for solving your problem. Then we know much better what to tell you to help you in the right direction. This goes for this question and all other Stack Overflow questions. – Ole V.V. Dec 22 '18 at 19:42

4 Answers4

1

You can look for in the values of the Map the Posts matching with the userId provided :

public List<Post> search(HashMap<String, Post> posts, String userId){
  return
  posts.values()
       .stream()
       .filter(p -> p.getUserId().equals(userId))
       .collect(toList());
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

This should do the trick,

posts.values().stream().filter(p -> p.userId.equals("yourUserId")).collect(Collectors.toList());
Sandeepa
  • 3,457
  • 5
  • 25
  • 41
1

With the current structure of the HashMap, there is no way to get posts for a userId but through iterating the whole map and comparing each values userId.

If you want to find all posts related to specific UserId efficiently without looping through the HashMap, then you have to change the structure of the HashMap itself and don't depend on the postId generated by the database as the key of the hashMap. Instead, you should use the userId as the key of the HashMap:

HashMap<String, ArrayList<Post>> posts = new HashMap<>();

Inserting:

public void addPost(String userId, Post newPost) {
    ArrayList<Post> postsForUserId = posts.get(userId);
    postsForUserId.add(newPost);
}

Retrieving:

public ArrayList<Post> getPosts(String userId) {
    return posts.get(userId);
}
Mohamed Ibrahim Elsayed
  • 2,734
  • 3
  • 23
  • 43
1

This can be done by changing Map structure.

If not necessary to have same structure of Map you have then changing Map for your special purpose will solve your problem.

//Initialization of map where key is userId and value is list of Post objects.
HashMap<String, List<Post>> postsByUserId = new HashMap<String, List<Post>>();

//Insertion of post into map. 
List<Post> postList = postsByUserId.get(post.userId);

//Null check and initialization of List.
if (postList == null) { 
    postList = new ArrayList<Post>();
    //Put list into map
    postsByUserId.put(post.userId, postList);
}
//Add object to the list. Either it will be the list retrieved from map or initialized above. 
postList.add(post);

//Retrieve list of post by userId
List<Post> postListOfUserId = postsByUserId.get(userId);

Thanks!

Prem
  • 316
  • 1
  • 5
  • 23