简体   繁体   中英

Adding a collection of collections to an ArrayList

I'm trying to make a program that takes a User class with parameters name, username, password and email to create a HashMap containing this information. The key will be the name of the User and the value will be an ArrayList containing information about the user (username, password, email). This class should provide a method to create a new user, which can be utilised from a PasswordDB class, which creates an Array of Users and their information by calling this method.

This is the code for the User class:

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

public class User
{
    private String name;
    private String username;
    private String password;
    private String email;    
    private ArrayList<String> arr = new ArrayList<String>();
    private HashMap<String,ArrayList<String>> userInfo = new HashMap<String,ArrayList<String>>();

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

    public HashMap<String, ArrayList<String>> addUser()
{        
    arr.add(username);
    arr.add(password);
    arr.add(email);
    userInfo.put(name, arr);
    return userInfo;
    }
}

And for the PasswordDB class:

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

public class PasswordDB
{
    private ArrayList<User> database;
    private User newUser;

public PasswordDB()
{
    ArrayList<User> database = new ArrayList<User>();
}

public void newUser(String name ,String username ,String password ,String email)
{
    User newUser = new User(name, username, password, email);
    database.add(newUser.addUser());
    }
}

And I receive the following error in the PasswordDB class: error

What am I doing wrong? How can I make this work? Thanks.

You are trying to add a HashMap into the ArrayList !

Try the following

public void newUser(String name ,String username ,String password ,String email)
  {
    User newUser = new User(name, username, password, email);
    newUser.addUser();
    database.add(newUser);
  }

change your PasswordDB class:

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

public class PasswordDB
{
    private ArrayList<HashMap<String, ArrayList<String>>> database;
    private User newUser;

public PasswordDB()
{
     database = new ArrayList<>();
}

public void newUser(String name ,String username ,String password ,String email)
{
    User newUser = new User(name, username, password, email);
    database.add(newUser.addUser());
    }
}

You can simplify this program a lot. A user does not need to know anything about any other user, so no list or database should be in the User class.

Have a User class that has properties for name, username, password, email and nothing else. It's just data about a user.

Then a PasswordDB class with your hashmap and just map a username to a user, you don't need all these extra lists.

class User {
    private String name;
    private String username;
    // ..

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

    public String getName() { return name; }
    // .. more getters
}

public class PasswordDB {
    private HashMap<String,User> userInfo = new HashMap<String,User>();

    public void addUser(User newUser) {
        userInfo.put(newUser.getUsername(), newUser);
    }

    // maybe you need to get a user by username
    public User getUser(String username) {
        return userInfo.get(username);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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