简体   繁体   English

获取属于hashmap中的一个对象的所有键

[英]getting all keys belonging to one object in a hashmap

Below is my user Class. 以下是我的用户类。 I also have a Card class which is the parent class of a Single currency card class and a multi card currency class. 我还有一个Card类,它是单一货币卡类的父类和多卡货币类。

I am fairly new to coding and have trouble understanding some concepts. 我对编码很新,并且无法理解一些概念。 What I need to do is return the cards that the user owns if the username and password match. 如果用户名和密码匹配,我需要返回用户拥有的卡。 This is in the getCards method. 这是在getCards方法中。 After this, i need to add the card to the Hashmap list if the username and password match. 在此之后,如果用户名和密码匹配,我需要将卡添加到Hashmap列表。 Any tips or other sites that would really help as I struggle a lot with the HashMap concept. 任何提示或其他网站真正有用,因为我与HashMap概念斗争很多。


public class User {
String username;
String password;
User user;

HashMap<String,Card> userHash = new HashMap <String, Card>(); //key is the cardID

public User(String username, String password)
{
    this.username = username;
    this.password = password;
}    

public String toString()
{
    return "User ---------" + "\n" + "Username: " + username + "\n" + "Password: " + password;
}

public String getUsername()
{
    return username;
}

public String getPassword()
{
    return password;
}

public boolean userValidate(String username, String password)
{
    if (username.contains(user.getUsername()) && password.contains(user.getPassword()))
    {
        System.out.println("User accepted");
        return true;
    }else
        System.out.println("Access denied");
    return false;
}

public HashMap<String, Card> getCards(String username, String password)
{
    for(String value : userHash.keySet())
        if (user.userValidate(username, password) == true)
        {
            //return user's cards
            return true;
        }else
            return null; 
        return null;

}


public boolean addCard(Card card, String username, String password)
{
    if(user.userValidate(username, password) == true)
    {
        user.getCards(username, password);

    }
    return false;
}

The card which belongs to the user

You can iterate over the map and add all cards to a list: 您可以迭代地图并将所有卡添加到列表中:

List<Card> list = new ArrayList();

for (Map.Entry<String, Card> entry : userHash.entrySet()) {
    if(entry.getKey().equals(username)) { list.add(entry.getValue(); }
}

Is that what you were looking for? 那是你在找什么?

Or if you already understood streams: 或者,如果您已经了解流:

userHash.entrySet().stream().filter(entry -> username.equals(entry.getValue())
                            .map(Entry::getKey)
                            .collect(toList());

Here is my design concept. 这是我的设计理念。

In this case, the user class can have many cards and each card has an id. 在这种情况下,用户类可以有许多卡,每张卡都有一个ID。 The validation check must be saved every time when creation user. 每次创建用户时都必须保存验证检查。

The userValidate method can be checked the arguments with its own values then save it to isvalid variable(boolean). userValidate方法可以使用自己的值检查参数,然后将其保存为isvalid variable(boolean)。

public boolean userValidate(String username, String password)
{
    if (username.contains(getUsername()) && password.contains(getPassword()))
    {
        System.out.println("User accepted");
        return isvalid = true;
    }else
        System.out.println("Access denied");
    return isvalid = false;
}

The getCards method is simple because it checks that the isvalid is true or not. getCards方法很简单,因为它检查isvalid是否为true。

public HashMap<String, Card> getCards() {
    if (isvalid) {
        // return user's cards
        return userHash;
    } else
        return null;

}

The addCard method, I can just save the card with card id, if user validation is success or nothing... addCard方法,如果用户验证成功或什么都没有,我可以用卡ID保存卡片...

public boolean addCard(Card card, String username, String password)
{
    if(userValidate(username, password))
    {
        userHash.put(card.getCardId(), card);
        return true;

    }
    return false;
}

So, come together with all these codes. 所以,与所有这些代码一起来。

import java.util.HashMap; import java.util.HashMap;

class Card 
{
    private String cardId;

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

}

public class User {
    private String username;
    private String password;
    private boolean isvalid;


    HashMap<String, Card> userHash = new HashMap<String, Card>(); // key is the
                                                                    // cardID

    public User(String username, String password) {
        this.username = username;
        this.password = password;

    }


    public boolean userValidate(String username, String password)
    {
        if (username.contains(getUsername()) && password.contains(getPassword()))
        {
            System.out.println("User accepted");
            return isvalid = true;
        }else
            System.out.println("Access denied");
        return isvalid = false;
    }

    public String toString() {
        return "User ---------" + "\n" + "Username: " + username + "\n" + "Password: " + password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public HashMap<String, Card> getCards() {
        if (isvalid) {
            // return user's cards
            return userHash;
        } else
            return null;

    }

    public boolean addCard(Card card, String username, String password)
    {
        if(userValidate(username, password))
        {
            userHash.put(card.getCardId(), card);
            return true;

        }
        return false;
    }


}

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

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