简体   繁体   English

是否有HashSet在Java中实现NavigableSet?

[英]Is there HashSet which implements NavigableSet in Java?

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. 用户用UserId表示,帖子用PostId表示,它们实际上是整数。 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: 该站点提供了一个api,该api返回以下帖子结构的列表:

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. 因此,我应该得到一个帖子,搜索我的用户集中是否已有帖子作者(用户),如果没有,则将新用户和帖子ID添加到用户集中,如果是-将帖子ID添加到该用户帖子的列表中。 Firstly I wanted to use HashSet - because of quick add and search methods. 首先,我想使用HashSet因为快速添加和搜索方法。 But then I realized that I can't get desired user from HashSet . 但是后来我意识到我无法从HashSet获得想要的用户。 Next I thought about TreeSet , which implements NavigableSet , so I can use TreeSet.ceiling(user) . 接下来,我想到了实现NavigableSet TreeSet ,因此可以使用TreeSet.ceiling(user) It returns object, so I can modify it. 它返回对象,所以我可以对其进行修改。 It all is fine, but HashSet is much faster than TreeSet . 一切都很好,但是HashSetTreeSet快得多。 So the question is, if there is some kind of HashSet which can return specific objects, like if it was implementing NavigableSet ? 所以问题是,是否存在某种HashSet可以返回特定对象,例如是否正在实现NavigableSet Or I just should rewrite my code somehow? 还是我应该以某种方式重写我的代码? Will be grateful for ideas. 将不胜感激。

The reason that HashSet is faster is that it's not sorted. HashSet更快的原因是它没有排序。 A NavigableSet needs to be sorted, and any implementation will necessarily have some overhead in keeping the set ordered. 需要对NavigableSet进行排序,并且任何实现都必须在保持集合有序方面有一些开销。 TreeSet is probably the best general-purpose sorted-set implementation available, and if you need the features that NavigableSet brings, you'll need to pay the performance cost. TreeSet可能是可用的最佳通用排序集实现,如果您需要NavigableSet带来的功能,则需要支付性能成本。 I'm quite skeptical that the overhead in the set lookups is the sticking point in your program in any case. 我很怀疑在任何情况下设置查找的开销都是程序的症结所在。

From the NavigableSet Javadoc, NavigableSet Javadoc中,

All Known Implementing Classes: ConcurrentSkipListSet , TreeSet 所有已知的实现类: ConcurrentSkipListSetTreeSet

And since a hash function destroys any kind of natural ordering I think a NavigableSet would be impossible with basic hashing. 而且由于散列函数会破坏任何自然顺序,因此我认为使用基本散列将无法实现NavigableSet

As an extension to my comment, this sounds like a practical use of HashMap. 作为我的评论的扩展,这听起来像是HashMap的实际使用。 You can use the user id in User as the key, and map to the User object. 您可以使用User中的用户ID作为键,并映射到User对象。 Using what you've given us, the below code would populate the users HashMap. 使用您提供的信息,以下代码将填充用户的HashMap。

HashMap<Integer, User> users = new HashMap<>();

for(Post post : posts) {
    int userId = post.getPostAuthorId();
    if(users.get(userId) != null) {
        users.get(userId).addPost(post);
    }
    else {
        users.put(userId, new User(userId));
    }
}

You should probably keep your list of Users and your list of posts separate. 您应该将用户列表和帖子列表分开。 Then use maps that suit your purposes to associate them. 然后使用适合您目的的地图进行关联。 If you use something like hibernate with it, you'll be able to keep using them as POJOs and get persistence out of it as well. 如果您使用类似休眠的方式,则可以继续将它们用作POJO,并从中获得持久性。

If you want to keep the posts inside the user you probably would also rewrite the Hash function of your User class so that the class would be ID'd by the userID alone and not the posts. 如果要将帖子保留在用户内部,则可能还会重写User类的Hash函数,以便仅由userID(而不是帖子)对类进行ID。 (As well as rewriting equals ) (以及重写equals

Some more on that here Why is the default hashcode() in java bad? 这里的更多信息为什么Java中的默认hashcode()不好?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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