I need to parse some site for a list of users and their posts. Users are represented by UserIds, and posts by PostIds, which are actually integers. So I create a class for user like this:
class User {
private int userId;
private List<Integer> postList;
//...
}
And then I'm encountering a problem. This site provides an api, which returns a list of post structures like:
class Post {
int postId;
int postAuthorId;
String postText;
//...
}
So I should get a post, search if I already have post author (user) in my user set, if no, add new user and post ids to user set, if yes - add post id to the list of this user posts.
Firstly I wanted to use HashSet
- because of quick add and search methods. But then I realized that I can't get desired user from HashSet
. Next I thought about TreeSet
, which implements NavigableSet
, so I can use TreeSet.ceiling(user)
. It returns object, so I can modify it. It all is fine, but HashSet
is much faster than TreeSet
.
So the question is, if there is some kind of HashSet
which can return specific objects, like if it was implementing NavigableSet
?
Or I just should rewrite my code somehow? Will be grateful for ideas.