简体   繁体   English

通过扩展自定义HashMap

[英]Custom HashMap by extending

Question: How do you create a custom HashMap class, extending HashMap, that can be manipulated like normal? 问题:如何创建自定义HashMap类,扩展HashMap,可以像平常一样进行操作?

Let me explain. 让我解释。 According to this question , you can create custom methods with ArrayLists by extending ArrayList and then writing your method. 根据这个问题 ,您可以通过扩展ArrayList然后编写方法来使用ArrayLists创建自定义方法。 How would I do that with HashMaps? 我怎么用HashMaps做到这一点?

I want to create a UserList class that can be initialized with UserList users = new HashMap<Users, Integer> . 我想创建一个UserList类,可以使用UserList users = new HashMap<Users, Integer>进行初始化。 This is my class for UserList so far: 到目前为止,这是我的UserList类:

public class UserList extends HashMap<Users, Integer> {

    public Users getUser(UUID uuid) {
        for (Users u: keySet()) {
            if (u.getUuid().equals(uuid))
                return u;
        }

        return null;
    }

    public Boolean containsPlayer(UUID uuid) {
        for (Users u: keySet()) {
            if (u.getUuid().equals(uuid))
                return true;
        }

        return false;
    }

    public Users removeUser(UUID uuid) {
        for (Users u: keySet()) {
            if (u.getUuid().equals(uuid)) {
                remove(u);
                return u;
            }
        }

        return null;
    }
}

But whenever I type private UserList listOfUsers = new HashMap<Users, Integer>; 但每当我键入私有UserList listOfUsers = new HashMap<Users, Integer>; in the main class, it brings up an incompatible type error. 在主类中,它会出现不兼容的类型错误。 How do I fix this? 我该如何解决? I remember learning about this before, but I've since forgotten. 我记得以前对此有所了解,但我已经忘记了。

Thanks 谢谢

UserList users = new UserList() , no need for anything else. UserList users = new UserList() ,不需要任何其他内容。

By extending the HashMap you already told the compiler that UserList is a HashMap . 通过扩展HashMap您已经告诉编译器UserListHashMap

As a suggestion, you may want to consider composition as an alternative design: Composition over inharitance 作为建议,您可能希望将构图视为一种替代设计: 构图而不是吸入

I know your question is about extending HashMap, but you can achieve the same by encapsulating the HashMap implementation inside your object: 我知道你的问题是关于扩展HashMap,但你可以通过在对象中封装HashMap实现来实现同样的目的:

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class UserList  {
    private Map<UUID, Users> users;

    public UserList() {
        this.users = new HashMap<>();
    }

    public Users getUser(UUID uuid) {
        return this.users.getOrDefault(uuid, null);
    }

    public Users removeUser(UUID uuui){
        return this.users.remove(uuui);
    }

    public Boolean containsPlayer(UUID uuid) {
        return this.users.keySet().contains(uuid);
    }

    public Users addUser(UUID uuid, Users users) {
        return this.users.put(uuid, users);
    }
}

class Users{}

class Main{
    public static void main(String[] args) {
        UserList userList = new UserList();
        UUID uuid = UUID.randomUUID();
        Users users = new Users();
        userList.addUser(uuid, users);
        userList.removeUser(uuid);
        userList.containsPlayer(uuid);
    }
}

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

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