How to use Map and List in JAVA?

Asked 2 years ago, Updated 2 years ago, 27 views

The progress you are currently learning does not use the database.

I'd like to create a membership form by creating a form with a thin builder.It's a little hard to do without using the database.

I heard that you can use List or Map... It's a little difficult to use these two.

I'm trying to use what I learned, but it's not easy.

HashMap list = new ArrayList<>(); What does it mean?

HashMap list = new HashMap()>(); What is this about? Is it possible to implement?

First of all, what I thought. If the key value of HashMap and any keyid in the value are the same, I want to delete and modify this ID... The value in the value is ID, password, name, gender, address...and so on.

Is there no need to make a keyid and check it? If the key value is correct, can I modify, delete, or log in?

Maybe it's because my head is complicated, but I feel like I'm mixed up. Please help me

java

2022-09-22 20:12

2 Answers

First of all, number 1 is wrong grammar in your question.

Use it like #2 or Map map = new HashMap();. Map is an interface, and HashMap is an implementation. They use it because of polymorphism.

If you use a data structure instead of a database, it would be better to use a map rather than a list.

For example, we can implement the following through the User class and Database class. I hope it was a good hint!

public class User {
    private String id;
    private String password;

    public getId(){
        return this.id;
    }
    //construct, get, tostring
}
public class Database {
    private static Map<String, User> users = new HashMap<String, User>();

    public static void addUser(User user) {
        users.put(user.getId(), user);
    }

    public static void removeUser(){};
    public static void updateUser(){};
    ....
}


2022-09-22 20:12

If you really want to do well, please learn "data structure/algebra" in advance.

You can solve it without any problems by sorting and searching by listing (linked, etc.), stacking, queue (de), tree, graph, and sorting and searching.


2022-09-22 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.